﻿/* onload settings */
$().ready(function () {
	//$('.product-list ul').equalHeights();
	$('.category-list ul').equalHeights();
	$('#main-nav ul li ul li:last-child').addClass('last');
	$('#product-photos ul li a,#product-photos .photo a').fancybox({
		'overlayShow':true,
		'enableEscapeButton':true,
		'hideOnOverlayClick':true,
		'hideOnContentClick':true
	});
	if($('#keyvisual').length > 0) startKVRotation();
	
	$('.upsell-services .servicewrap:nth-child(5n + 5)').addClass('right');
	$('.upsell-services .servicewrap').mouseover(function() {
		$(this).addClass('active');
	}).mouseout(function(){
		$(this).removeClass('active');
	});
	
	/* set heights */
	if($('.upsell-services .servicewrap').length > 0) {
		servicelist = $('.upsell-services .servicewrap').length;
		var rows = Math.ceil(parseInt(servicelist) / 5);
		var from = 0;
		var to = 5;
		var i = 0;
		for (i=0;i<=rows;i++) {
			var current_row = $('.upsell-services .servicewrap').slice(from,to);
			equalHeight(current_row);
			from= from + 5;
			to= to + 5;
		}
	}


    /* facebook like button support */
    if ($('#fb-root').length != 0) {
        FB.init({appId  : '5b97795b28336e6c52115070cd993cef', status : true, cookie : true, xfbml  : true});

		$('#fbLogin').show();
		$('#fbLogin').html($('#fbLogin').html() + ' Prijavi se in sodeluj v <a href="http://www.facebook.com/FMCStore?sk=app_166000816788179">nagradni igri</a>!');
        $('#fbLike').hide();

		FB.getLoginStatus(function(response) {
			if (response.session) {
				$('#fbLogin').hide();
				$('#fbLike').show();
			} /*else {
				$('#fbLogin').show();
            	$('#fbLike').hide();
			}*/
		});

        FB.Event.subscribe('edge.create', function(href) {
            countLike(href);
        });

		FB.Event.subscribe('auth.sessionChange', function(href) {
			$('#fbLogin').hide();
            $('#fbLike').show();
        });
    }

    var countLike = function(href) {
        FB.api('/me', function(response) {
            var name = response.name;
            var email = response.email;
            $.ajax({
                url: '/facebook/fbcount',
                data: { name : name, email : email, page: href}
            });
        })
    };
});

function equalHeight(els) {
	var tallest = 0;
	els.each(function() {
		var thisHeight = $(this).find('h4').height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	els.find('.service').height(tallest);
	els.height(tallest+20);
}

/* external links */
$('a[rel*=external]').click( function() {
	window.open(this.href);
	return false;
});

$('.social-links li.sc span').click(function() {
	if($(this).parent().hasClass('sc-on')) {
		$(this).parent().removeClass('sc-on');
	} else {
		$(this).parent().addClass('sc-on');
	}
});

/* disabled look onmouseover */
$(".upsell-services").mouseover(function() {
	$(this).addClass('upsell-services-hover');
}).mouseout(function(){
	$(this).removeClass('upsell-services-hover');
});


/* more/less text */
$(function() {
	// The height of the content block when it's not expanded
	var adjustheight = 125;
	// The "more" link text
	var moreText = "Pokaži tekst";
	// The "less" link text
	var lessText = "Skrij tekst";

	// Sets the .more-block div to the specified height and hides any content that overflows
	$('.more-less .more-block').css('height', adjustheight).css('overflow', 'hidden');

	// The section added to the bottom of the "more-less" div
	$('.more-less').append('<span id="adjust"><span></span></span>');
	$('#adjust span').text(moreText);
	$('#adjust').toggle(function() {
		$(this).parents("div:first").find(".more-block").css('height','auto').css('overflow','visible');
		$(this).addClass('open');
		$('#adjust span').text(lessText);
	}, function() {
		$(this).parents("div:first").find(".more-block").css('height',adjustheight).css('overflow','hidden');
		$(this).removeClass('open');
		$('#adjust span').text(moreText);
	});
});

/* show / hide forms */
$('.loginform .sw').click(function() {
	$('.loginform').hide();
	$('.forgotpassword').show('slow');
});
$('.forgotpassword .sw').click(function() {
	$('.forgotpassword').hide();
	$('.loginform').show('slow');
});

/* execute at modal window launch */
function modalLoaded() {
	$('.change-address ul').equalHeights();
}
/* modal window launch */
if($('.new-window').length) {
	$('#lightbox').jqm({ajax: '@href', trigger: 'a.new-window', onLoad: modalLoaded});
}

/* keyvisual toggle */
$('#keyvisual .sw li').click(function () {
	if($(this)[0].className == 'active') return false;
	var active = $(this)[0].className;
	$('#keyvisual .sw .active').removeClass('active');
	$('#keyvisual .sw .' + active).addClass('active');
	$('#keyvisual .kv .active').fadeOut(200).delay(200).removeClass('active');
	$('#keyvisual .kv .' + active).fadeIn(500).addClass('active');
	/* reset rotation timing */
	clearInterval(KVRotation);
	startKVRotation();
});

/* keyvisual rotate */
function startKVRotation() {
	KVRotation = setInterval('rotateKV()',8000);
}
function rotateKV() {
	var activeKV = $('#keyvisual .kv .active');
	var activeSW = $('#keyvisual .sw .active');
	activeKV.fadeOut(200).removeClass('active');
	activeSW.removeClass('active');
	if(activeKV.next().length > 0) {
		activeKV.next().fadeIn(500).addClass('active');
		activeSW.next().addClass('active');
	} else {
		$('#keyvisual .kv li:first-child').fadeIn(500).addClass('active');
		$('#keyvisual .sw li:first-child').addClass('active');
	}
}

function closeAddressPicker() {
    $('#lightbox').jqmHide();
    var l = window.location.href;
    window.location.href = l;
    return false;
}
