function show_pic(src, st) {
	$("#modelphoto"+st).attr("src", src);
};

;$(document).ready(function () {
	
	// AMLbar
	if ($("#aml_search_box").length != 0) {
		$('#aml_search_box #id_q').val('išči')
		$('#aml_search_box #id_q').bind('keyup', function(e) {
			var code = (e.keyCode ? e.keyCode : e.which);
			if(code == 13) {
				$('#aml_search_box').submit();
			}
		});
		$('#aml_search_box #id_q').click(function(){
			$(this).val("");
			$(this).css({'width':'150px'});
			$(this).css({'background-color': '#ddd','color': '#000'});
		});
	}
	
	// COMPARE.HTML
	// GET PHONE MODELS FROM BRANDS
	$(".brand").change(function () {
		$(".txt").hide();
		var id = $(this).attr("id");
		var st = id.replace("brand", "");
		var brand = $(this).attr("value");
		var models = brands[brand];// DEFINE brands IN PAGE BODY!
		var new_options = "<option value=\"0\">Izberi model ...</option>";
		for(var i=0; i < models.length; i++){
			new_options += "<option value=\"" + models[i][0] + "\" onmouseover=\"show_pic('" + models[i][2] + "','"+ st +"');\">" + models[i][1] + "</option>";
		}
		$("#model"+st).removeAttr('disabled').html(new_options);
	});

	// GET PHONE PIC FROM MODEL
	$(".model").change(function () {
		var id = $(this).attr("id");
		var st = id.replace("model", "");
		var brand = $('#brand'+st).attr('value');
		var models = brands[brand]; // DEFINE brands IN PAGE BODY!
		var model = $(this).attr("value");
		for(var i=0; i < models.length; i++){
			if(models[i][0] == model) {
				$("#modelphoto"+st).attr("src", models[i][2]);
			}
		}
		sst = parseInt(st) + 1;
		$('#brand' + sst).prop("disabled", false);
	});
	
	$("#model2").change(function () {
		$('#frontCompare').submit();
	});
	
	// TESTS
	// GET PHONE MODELS FROM test BRANDS
	$("#test").change(function () {
		var brand = $(this).attr("value");
		var models = tests[brand]; // DEFINE tests IN PAGE BODY!
		var new_options = "<option value=\"0\">Izberi model ...</option>";
		var has_article = false;
		for(var i=0; i < models.length; i++){
			has_article = true;
			new_options += "<option value=\"" + models[i][2] + "\">" + models[i][1] + "</option>";
		}
		if (!has_article) {
			new_options = "<option value=''>Trenutno še ni testov</option>";
			$("#testmodel").attr("disabled", "disabled").html(new_options);
		} else {
			$("#testmodel").removeAttr('disabled').html(new_options);
		}
	});

	// GET PHONE test URL FROM MODEL
	$("#testmodel").change(function () {
		var permalink = $(this).attr("value");
		window.location.replace(permalink);
	});
	
	// COMPARE box shrink
	$(".title a").click(function () {
		var prop_names = $(this).attr("id");
		$(prop_names).hide();
		$(this).blur();
		
		return false;
	});
	
	/* sticky div 
	if ($("#stickyfloat").length != 0) {
		$('#stickyfloat').stickyfloat({lockBottom:false});
	}
	*/
	
	/* VIP caruzl */
	if ($("#vip_caruzl").length != 0) {
		function onSwitch(curr, next, opts){
			var n = opts.currSlide;
			$("#featuredNav li").eq(n-1).removeClass("selected");
			$("#featuredNav li").eq(n).addClass("selected");
		}
		
		$('#vip_caruzl').cycle({
			speed: 500,
			timeout: 10000,
			after: onSwitch,
			pause: 1,
			pauseOnPagerHover: 1
		})
		
		$("#featuredNav a").each(
			function(intIndex){
				$(this).bind("mouseenter", function(){
					$(this).parent().siblings().removeClass("selected");
					$(this).parent().addClass("selected");
					$('#vip_caruzl').cycle({startingSlide:intIndex,timeout:0});
				});
		});
	}
	
	/* VIP caruzl */
	if ($("#cycle").length != 0) {
		$('#cycle').cycle({
			speed: 500,
			timeout: 10000,
			pause: 1
		})
	}
	
	/* NAVIGATION */
	if ($('.nav').length != 0 ) {
		$('.nav > ul > li').each(
			function(intIndex){
				$(this).bind("mouseenter", function(){
					if ($('.testi').length != 0 ) {
						$('.testi').remove();
					}
					$(this).siblings().removeClass("selected");
					$(this).addClass("selected");
					$(this).siblings().children('.article_list').hide();
					$(this).children('.article_list').show();
				});
			});
		
		$('#logo').hover(
			function () {
				$('.nav').delay(100).show();
				// $('.contentWrap').delay(500).fadeTo('fast',0.18);
		});
		$('#logo').bind("mouseup", function (){
			$('.nav').hide();
		});
		$('.contentWrap').bind("mouseenter", function (){
			// $('.contentWrap').fadeTo('fast',1);
			$('.nav').hide();
		});

		$('.wrap').bind("mouseup", function (){
			// $('.contentWrap').css({'opacity':1});
			$('.nav').hide();
		});
		
		// text trimming plugin
		// $('.section.main .article_list p').ellipsis();
	}
	
	if ($('.lastestReviewsHeader').length != 0 ) {
		// nav
		$('#filter_znamka').click(function () {
			$(this).blur();
			$(this).toggleClass('selected');
			$('#latestBrandList').toggleClass('hidden');
			return false;
		});
		$('#filter_os').click(function () {
			$(this).blur();
			$(this).toggleClass('selected');
			$('#latestOsList').toggleClass('hidden');
			return false;
		});
		// ajax
		$('.latestOpt li a').click(function () {
			$(this).blur();
			// $('.latestOpt li a').removeClass('selected');
			$(this).toggleClass('selected');
			$('.lastestReviews').css({'opacity':.5})
			$('#latest_loading').show();
			
			var brands = "";
			$.each($('#latestBrandList a.selected'), function(i,val){ brands = brands + $(this).attr('id'); } );
			var oses = "";
			$.each($('#latestOsList a.selected'), function(i,val){ oses = oses + $(this).attr('id'); } );
			
			var name = $(this).attr('id');
			$.get(
				'/primerjava/latest_x/', 
				{'brands': brands, 'oses': oses },
				function(data) {
					$('.lastestReviews').html(data).css({'opacity':1});
					$('#latest_loading').hide();
			});
			
			return false;
			
		});
	}
	
	// if ($('#stickyBox').length != 0 ) {
	// 	$(window).bind("scroll", function(event) {
	// 		if( ($(document).height() -$(window).height() - $(document).scrollTop() < 300) ) {
	// 			$('#stickyBox').slideDown('fast');
	// 		}
	// 		if( ($(document).height() - $(window).height() - $(document).scrollTop() > 300) ) {
	// 			$('#stickyBox').slideUp();
	// 		}
	// 	});
	// 	$('.closeRelated').bind("mouseup", function(event) {
	// 		$('#stickyBox').remove();
	// 	});
	// }
	
	// okrajsevalnik
	if ($('.app').length != 0 ) {
		$('.app h3').ellipsis();
	}
	if ($('.video').length != 0 ) {
		$('.video h3').ellipsis();
	}
	if ($('.promoted').length != 0 ) {
		$('.promoted li h3').ellipsis();
	}

	//	twitter bottom
	if ($("#twitter-feed").length != 0) {
		$("#twitter-feed").getTwitter({ 
			userName: "connectSlo", 
			numTweets: 15, 
			loaderText: "Samo trenutek...", 
			slideIn: false, 
			showHeading: false, 
			headingText: "", 
			showProfileLink: true
		});
	}
	
	//  RSS scroll
	if ($('#aside .latestRSS').length != 0 ) {
		var max_offset = $('.contentWrap.bottom').height() - $('#aside').height();
		var lt = $('.latestRSS').offset().top;
		$(window).bind("scroll", function(event) {
			var wt = $(window).scrollTop()
			if (wt > lt) {
				var top_offset = wt - lt;
				if (top_offset > max_offset) {
					top_offset = max_offset;
				}
				$('.latestRSS').css({"marginTop": (top_offset) + "px"});
			}
		});
	}

});
