﻿/* onload settings */
$().ready(function () {
	$('body').removeClass('nojs');
	$('#slider .photos img').hide();
	$('#slider .photos img:first-child').show();
	$('#slider .nav li:first-child').addClass('active');
	
	introSlide();
	checkHeight();
	$(window).resize(function() {resizeBack()});
	$(window).resize(function() {checkHeight()});
	
	/* customized fancybox */
	$("a.lightbox").fancybox({
		'transitionIn' : 'fade',
		'transitionOut' : 'fade',
		'speedIn' : 600,
		'speedOut' : 200,
		'overlayShow' : false,
		'titlePosition' : 'over',
		'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
		    // return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
			var fb_slides = '';
			for(i = 0; i < currentArray.length; i++) {
				if(currentIndex == i) {
					fb_slides = fb_slides + '<span class="active">' + currentArray[i].title + '</span>';
				} else {
					fb_slides = fb_slides + '<span>' + currentArray[i].title + '</span>';
				}
			}
		    return '<span id="fancybox-title-over"><div class="titles"> ' + fb_slides + '</div></span>';
		},
		ajax : {
			beforeSend : function(){
				if($('#fancybox-outer')) $('#fancybox-outer').removeClass('floater');
			}
		}
	});

	$('a.floater').each(function(){
		var id = $(this).attr('id');
		var formid = id.replace('buySubmit_','addcart_buy_');
		$(this).fancybox({
			'transitionIn'			:	'fade',
			'transitionOut'			:	'fade',
			'speedIn'				:	600, 
			'speedOut'				:	200, 
			'overlayShow'			:	true,
			'width'        			:   522,
			'height'       			:   131,
			'autoScale'    			:   false,
			'showCloseButton' 		:	false,
			'showNavArrows' 		:	false,
			'titleShow' 			: 	false,
			'type' 					: 	'ajax',
			'overlayOpacity'		:	0.7,
			'overlayColor'			:	'#000',
			'hideOnOverlayClick'	:	false,
			ajax : {
				beforeSend : function(xhr, settings){
					settings.data = $('#'+formid).serialize();
					if(validate($('#'+id.replace('buySubmit_','addcart_buy_'))[0]) == false){
						xhr.abort();
						if($('#fancybox-loading')) $('#fancybox-loading').css('display','none');
					}
					if($('#fancybox-outer')){
						$('#fancybox-outer').addClass('floater');
						$('#fancybox-outer').addClass(mplang);
					}
				},
				type	: "POST",
				data	: $('#'+formid).serialize()
			}
		});
	});
	if($("#customsel")) $("#customsel").selectbox();				
});

/* external links */
$(function() {
	$('a[href][rel*=external]').each(function(i){
		this.target = "_blank";
	});
});

/* switch product photos */
$('#slider .nav li span').click(function() {
	if($(this).parent().is('.active')) {
		return false;
	}
	/* wait until animation stops */
	if($('#slider .photos').children().is(':animated')) {
		return false;
	}
	$('#slider .nav li').removeClass('active');
	$(this).parent().addClass('active');
	var active = $(this).parent()[0].className.substr(0,2);
	$('#slider .photos img').fadeOut(500).delay(500);
	$('#slider .photos .' + active).fadeIn(800);
});

function inputFocus(el){
	if (el.value == el.defaultValue){
		el.value = '';
	}
}
function inputBlur(el){
	if (el.value == ''){
		el.value = el.defaultValue;
	}
}

function toggleShipping(){
	$('#shipping_form').toggleClass('dn');
	if($('#billing_shipping').is(':checked')){
		$('.shippval').removeAttr('valid');
		$('.shippvalnum').removeAttr('valid');
	}
	else {
		$('.shippval').attr('valid','req');
		$('.shippvalnum').attr('valid','req num');
	}
}

function submitForm(id,checkcard){
	if(checkcard && $('#gcinput')){
		$.ajax({
			type: 'post',
			url: mppath+'/'+mplang+'/sys/add-giftcard',
			data: $('#'+id).serialize(),
			success: function(data){
				if(data == 'ok'){
					if(validate($('#'+id)[0])) $('#'+id).submit();
				}else{
					validate($('#'+id)[0]);
					$('#gcinput').addClass('fail');
					if($('#giftcard_err')){
						$('#giftcard_err').css('display','block');
						$('.status').css('display','block');
					}
				}
			}
		});
	}
	else if(validate($('#'+id)[0])) $('#'+id).submit();
	return false;
}

var timer;
var curritem = 1;
var nextitem = 2;
var numitems = 0;
function introSlide(){
	resizeBack();
	timer = setTimeout("showIntroHidden()",3000);
	if($('.introslide').length > 0){
		numitems = $('.introslide').length;
		timer = setTimeout("toggleIntroSlide(nextitem)",6000);
	}
	
	$('a.introsllnk').each(function(index){
		$(this).click(function(e){
			e.preventDefault();
			idpart = $(this).attr('id').split("_");
			if(idpart[1] != curritem) toggleIntroSlide(idpart[1]);
		});
	});
}

function resizeBack(){
	width = $(window).width();
	height = $(window).height();
	razmerje = $('.slideimg').width() / $('.slideimg').height();

	if(width > 1180) towidth = width;
	else towidth = 1180;
	toheight = towidth / razmerje;
	
	if(toheight < height) {
		$('.slideimg').height(height);
		$('.introfootcont').height(height);
		$('.container-intro').height(height);
		towidth = razmerje * height;
		$('.slideimg').width(towidth);
		$('.introfootcont').width(towidth);
		$('.container-intro').width(towidth);
	}
	else{
		$('.slideimg').width(towidth);
		$('.slideimg').height(toheight);
		$('.introfootcont').width(towidth);
		$('.introfootcont').height(toheight);
		$('.container-intro').width(towidth);
		$('.container-intro').height(toheight);
	}
}

function toggleIntroSlide(itemindex){
	clearTimeout(timer);
	nextitem = itemindex;
	if(nextitem > numitems) nextitem = 1;
	$('#introslide_'+curritem).fadeOut(1500,'linear');

	curritem = nextitem;
	
	$('img.introslbuttimg').attr('src',($('img.introslbuttimg').attr('src').replace('on','off')));
	$('#introslbutt_'+curritem).attr('src',($('#introslbutt_'+curritem).attr('src').replace('off','on')));
	
	$('#introslide_'+curritem).fadeIn(1500,'linear');
	nextitem++;
	timer = setTimeout("toggleIntroSlide(nextitem)",9000);
}

function showIntroHidden(){
	$('.introfoot').fadeIn(2000,'linear');
	$('.site-intro').fadeIn(2000,'linear');
}

function checkHeight(){
	if($('#checkheight')){
		windowheight = $(window).height();
		elementheight = $('#checkheight').height();
		if(elementheight < windowheight) $('#checkheight').css('height',windowheight);
	}
}
