/*==============================================================================
	:first-child, :last-child gibi selectorları desteklemeyen IE'lere kolaylık.
==============================================================================*/
$(function(){

	$("ul li:first-child").addClass("first");
	$("ul li:last-child").addClass("last");
	
	$("ul li:nth-child(odd), .result-set:nth-child(odd),").addClass("odd");
	$("ul li:nth-child(even), .result-set:nth-child(even)").addClass("even");
});


/*==============================================================================
	Index sayfasındaki vizyon filmleri scrollerını başlat.
==============================================================================*/
$(function(){
	
	var container	= $("#content-top"),
		scroller	= $(".scroller", container),
		btnNext		= $(".btnNext", container),
		btnPrev		= $(".btnPrev", container);
	
	scroller.jCarouselLite({
		'auto'		 : 3000,
		'btnNext'	 : btnNext,
        'btnPrev'	 : btnPrev,
		'mouseWheel' : true,
		'speed'		 : 250,
		'visible'	 : 7
	});
	
});


/*==============================================================================
	Portreler kutusu scrollerını başlat.
==============================================================================*/
$(function(){
	
	var container	= $(".portraits-box"),
		scroller	= $(".scroller", container),
		btnNext		= $(".btnNext", container),
		btnPrev		= $(".btnPrev", container);
	
	scroller.jCarouselLite({
		'btnNext'	 : btnNext,
		'btnPrev'	 : btnPrev,
		'mouseWheel' : true,
		'speed'		 : 250,
		'visible'	 : 3
	});

});


/*==============================================================================
	Fotoğraf kutusu scrollerını başlat.
==============================================================================*/
$(function(){
	
	var container	= $(".image-gallery-box"),
		scroller	= $(".scroller", container),
		btnNext		= $(".btnNext", container),
		btnPrev		= $(".btnPrev", container);

	if (!container.parents().hasClass("movie-details")) {

		scroller.jCarouselLite({
			'btnNext'	 : btnNext,
			'btnPrev'	 : btnPrev,
			'mouseWheel' : true,
			'speed'		 : 250,
			'visible'	 : 10
		});

	};

});


/*==============================================================================
	Index sayfasındaki haber sliderını başlat.
==============================================================================*/
$(function(){
	
	var container	= $("#headlines"),
		titles		= $("a span", container);
		
	for ( var l = titles.length; l >= 0; l-- ) {
		var title = titles.eq(l);
		if ( !title.hasClass("title") && !title.hasClass("spot") && title.children(".title").length > 0 && title.children(".spot").length == 0 ) {
			title.children(".title").css("height", 50);
		}
	}
	
	container.coinslider({
		'width'			: 604,		// width of slider panel
		'height'		: 325,		// height of slider panel
		'spw'			: 8,		// squares per width
		'sph'			: 4,		// squares per height
		'delay'			: 3000,		// delay between images in ms
		'sDelay'		: 30,		// delay beetwen squares in ms
		'opacity'		: 1,		// opacity of title and navigation
		'titleSpeed'	: 0,		// speed of title appereance in ms
		'effect'		: "rain",	// random, swirl, rain, straight
		'navigation'	: true,		// prev next and buttons
		'links' 		: true,		// show images as links
		'hoverPause'	: true		// pause on hover
	});
	
});

/*==============================================================================
	Yine :nth-type selectorlarını kullanamadığımız IE'ler için hack.
==============================================================================*/
$(function(){
	$(".series-box .news-list li, .people-box .people-grid li").slice(-2).css('border', "none");
});

/*==============================================================================
	Film detay sayfasındaki ekstra bilgi paneli animasyonu
==============================================================================*/
$(function(){
	
	var container = $(".movie-details .extra-info"),
		opener	  = $("a.opener", container),
		panel	  = $(".info", container);
			
	panel.css('opacity', 0);
		
	opener.bind("click", function(e){
		
		e.preventDefault();
		
		var height = panel.height();
		
		opener.remove();
		container.animate({ 'height': height }, 250);
		panel.animate({ 'opacity': 1 }, 500);
	});
	
});


/*==============================================================================
	Detaylı arama barı.
==============================================================================*/
$(function(){
	
	var container	= $(".advanced-filter"),
		panels		= $(".filter", container);

	panels.each(function(){
		
		var panel = $(this);

		if ( ~panel.attr("class").indexOf("items-") ) {

			var items		= $("li", panel),
				itemCount	= panel.attr("class").split("items-")[1].split(" ")[0],
				opener		= $("a.more", panel),
				height		= opener.outerHeight();

			itemCount = ( itemCount > items.length ? 0 : itemCount );

			if ( itemCount && opener.length === 1 ) {

				for ( var i = 0; i < itemCount; i++ ) {
					height += items.eq(i).outerHeight();
				};

				items.slice(itemCount).css('opacity', 0);
				
				opener.css({
					'bottom': 0,
					'left': 0,
					'position': "absolute",
					'width': opener.width()
				});

				panel.data("state", [ false, height, ($("ul", panel).outerHeight() + opener.outerHeight()) ]);

				panel.height(height);

				opener.bind("click", function(e){
					
					e.preventDefault();

					var state = panel.data("state");

					panel.animate({
						'height': (state[0] ? state[1] : state[2])
					}, function(){
						panel
						.removeClass("open closed")
						.addClass(!state[0] ? "open" : "closed")
						.data("state", [ !state[0], state[1], state[2] ]);
					});

					items.slice(itemCount).animate({
						'opacity': (state[0] ? 0 : 1 )
					}, 750);

				});

			}
		}

	});

});


/*==============================================================================
	Header ve footerdaki dropdown menü scripti.
==============================================================================*/
$(function(){
	
	var dropdowns = $(".c-dropdown");

	dropdowns.each(function(){
		
		var dropdown	= $(this),
			options		= $(".options", dropdown).eq(0),
			width		= options.css("width"),
			height		= 0,
			items		= $("ul li", options),
			itemCount	= 0,
			itemHeight	= $("ul li a", options).eq(0).outerHeight();
		
		dropdown.css({
			'height': dropdown.find(".dropdown").outerHeight(),
			'width': dropdown.find(".dropdown").outerWidth()
		});

		dropdown.find(".dropdown").css({
			'position': "absolute",
			'left': 0,
			'top': 0,
			'white-space': "nowrap"
		});

		if ( $(".options > ul", dropdown).length === 1 ) {
			width = $(".options > ul", dropdown).width();
		} else if ( $(".options > ul", dropdown).length > 0 ) {
			width = 0;
			for (var l = $(".options > ul", dropdown).length - 1; l >= 0; l--) {
				width += parseInt($(".options > ul", dropdown).eq(l).outerWidth(true));
			};
		} else {
			width = "auto";
		}
		
		if ( ~dropdown.attr("class").indexOf("c-options-") ) {
			itemCount = dropdown.attr("class").split("c-options-")[1].split(" ")[0];
			itemCount = ( itemCount > items.length ? 0 : itemCount );
		}

		options.css({
			'display': "block",
			'opacity': 0,
			'overflow': "auto",
			'visibility': "hidden",
			'width': ( itemCount > 0 ? width + 20 : width )
		});
		
		if ( itemCount ) {
			height = itemCount * itemHeight;
		} else {
			height = options.height();
		}
		
		options.css({
			'display': "none",
			'height': 0,
			'visibility': "visible"
		});

		dropdown.openFn = function () {
			dropdown.addClass("c-dropdown-active");
			dropdown.data("isOpen", true);
			options.css('display', "block");
			options.stop().animate({
				'height': height,
				'opacity': 1
			}, 200);
		};

		dropdown.closeFn = function () {
			dropdown.removeClass("c-dropdown-active");
			dropdown.data("isOpen", false);
			options.stop().animate({
				'height': 0,
				'opacity': 0
			}, 200, function () {
				options.css('display', "none");
			});
		}

		dropdown.find(".dropdown").bind("click", function(){
			
			if ( !dropdown.data("isOpen") ) {
				dropdown.openFn();
			} else {
				dropdown.closeFn();
			}

		});

		$(document).bind("click", function(e) {
			if ( dropdown.data("isOpen") && e.target != dropdown.find(".dropdown")[0] ) {
				dropdown.closeFn();
			}
		});

	});
});


/*==============================================================================
	Custom radio inputlar.
==============================================================================*/
$(function(){
	
	$("label.radio").live("click", function(){
		var label = $(this),
			name = $("input:radio", label).attr("name"),
			klass = "selected";
		
		if ( name && name.length > 0 ) {
			$("input:radio[name=" + name + "]").parent("label").removeClass(klass);
			$("input:radio[name=" + name + "]:checked").parent("label").addClass(klass);
		}
	});

});

