(function ($) {
	$.fn.vAlign = function() {
		return this.each(function(i)
		{
			var ah = $(this).find(".thumbnail-image img").height();
			var ph = $(this).find(".thumbnail-image").height();
			var mh = (ph - ah) / 2;
			if(mh>0) 
			{
				$(this).find(".thumbnail-image img").css('margin-top', mh);
			}
			else
			{
				$(this).find(".thumbnail-image img").css('margin-top', 0);
			}
		});	
	}
})(jQuery);

$(function() {
	$.autoScroll({
        scrollDuration: 1000, 
        showDuration: 600, 
        hideDuration: 300
    });
		    
	$(".random-image").cycle({ 
    fx:      'scrollDown', 
    speed:    600, 
    timeout:  2000 
	});
	// superfish 
	$('.sf-menu').superfish();
	///$(".thumb-align").vAlign();
	// sub categories
	/*$('.widget ul ul').each(function() { $(this).css('display', 'none'); });*/
	$('.widget ul > li > span.icon-generic-1.hasChild').click(function() {
		$childs = $(this).parents('li:first').find('ul:first');
		if (($childs).is(':visible')) {
			$childs.hide();
		} else {
			$childs.show();
		}
		return false;
	});
	$('.widget ul > li > span.bullet.hasChild').click(function() {
		$childs = $(this).parents('li:first').find('ul:first');
		if (($childs).is(':visible')) {
			$childs.hide();
		} else {
			$childs.show();
		}
		return false;
	});
	$('.widget ul > li > span.bullets.hasChild').click(function() {
		$childs = $(this).parents('li:first').find('ul:first');
		if (($childs).is(':visible')) {
			$childs.hide();
		} else {
			$childs.show();
		}
		return false;
	});
	$('.fancybox').fancybox();
	$('.close-banner').click(function(){
		$(this).parent().empty();
	});

	$('.add-cart-button').fancybox({type:'iframe',width:500,height:430});
	$('.add-cart-image').fancybox({type:'iframe',width:500,height:430});
	
	$('.balloon-detail').balloon({ 
		position: "top right",
		minLifetime: 2500,
		css : {
			minWidth: "20px",
			padding:"5px",
			borderRadius: "3px",
			border: "none",
			boxShadow: "0 0 4px 2px #333",
			color: "#fff",
			backgroundColor: "#5a7179",
			opacity: "0.85",
			zIndex: "32767",
			textAlign: "left"
		}
	});
	
	$(".image-view").click(function(){
		$(".view-image-detail").trigger('click');
	});
	$(".view-image-detail").fancybox({
		'width'				: '50%',
		'height'			: '75%',
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'changeSpeed' :999,
		'onComplete'	:	function() {
			//alert('a')
		}
	});
	$('.image-view').fancybox();
	
});

(function(jq) {
    jq.autoScroll = function(ops) {
        ops = ops || {};
        ops.styleClass = ops.styleClass || 'scroll-to-top-button';
        var t = jq('<a class="'+ops.styleClass+'">Top</a>'),
            d = jq(ops.target || document);
        jq(ops.container || 'body').append(t);

        t.css({
            opacity: 0,
            position: 'absolute',
            top: 0,
            right: 10
        }).click(function() {
            jq('html,body').animate({
                scrollTop: 0
            }, ops.scrollDuration || 1000);
        });

        d.scroll(function() {
            var sv = d.scrollTop();
            if (sv < 10) {
                t.clearQueue().fadeOut(ops.hideDuration || 200);
                return;
            }
            else
            {
	            var sv = (d.scrollTop()+($(window).height()-30));
            }

            t.css('display', '').clearQueue().animate({
                top: sv,
                opacity: 0.8
            }, ops.showDuration || 3000);
        });
    };
})(jQuery);

function setHomepage(url)
{
	if (document.all)
	{
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(url);
	}
	else if (window.sidebar)
	{
		if(window.netscape)
		{
			try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
			}
			catch(e)
			{
				alert("this action was aviod by your browser, if you want to enable, please enter about:config in your address line,and change the value of signed.applets.codebase_principal_support to true");  
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage', url);
	}
}

function bookMark(url, title)
{
	bookmarkurl = url;
	bookmarktitle = title;
	if (document.all)
	window.external.AddFavorite(bookmarkurl,bookmarktitle)
	else if (window.sidebar) // firefox
	window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
}

function submit_lang(lang)
{
	document.getElementById("lang_page").value = lang;
	document.getElementById("mainsearchfrm").setAttribute("action", "/index.php?lang_page="+lang);
	document.getElementById("mainsearchfrm").submit();
}

function copy_codeads(theField) {
	var tempval=eval("document.form_codeads.codeads")
	tempval.focus()
	tempval.select()
	therange=tempval.createTextRange()
	therange.execCommand("Copy")
}

function validateEmail($email) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	return emailReg.test( $email );
}

$( document ).ready(function() {
	$('#email-form').submit(function(){
	    if($('#newsletter').val() == '')
	    {
	    	alert("Please fill Email");
	    	$('#email-form #newsletter').focus(); 
	    	return false;
	    }
	    if(validateEmail($('#newsletter').val()) == false)
	    {
	    	alert("Email is incorrect");
	    	$('#email-form #newsletter').focus(); 
	    	return false;
	    }
	}); 
});