var Contacts = {

    clearField : function (item)
    {
        if($(item).val() == $(item).attr('title'))
		{
			$(item).attr('value', '');
		}
    },

    sendM : function (thisFom)
    {
        Contacts.actualForm = thisFom;

        $.post("/",
            'Func=contacts.sendMessage&'+$(thisFom).serialize(),
            function(data) {

                if (data.error)
                {
                    jQuery.each(data.items, function(i, val) {
                        $('#'+i).val(val);
						$('#'+i).attr('title', val);
                    });
                }
                else
                {
                    Contacts.actualForm.reset();
					$('#contact-msg').html(data.message);
                }
            },
		"json"
		);
    }
};

var Products = {
    setFav : function(id,language)
	{
		$.post("/", 
			'Func=products.favourites.setFav&id='+id+'&language='+language, 
			function(data)
			{
				window.location.reload();
			},
			"json"
		);
	},
	
	makeSelect : function(selectId)
	{
		var selectIdPriv = '.'+selectId;
		var designString = new Array;
		var currentLink;
		$(selectIdPriv).hide();
		$('<ul class="'+selectId+'-list"></ul>').attr('style','position:absolute;left:0px;').insertAfter(selectIdPriv);
	
		$(selectIdPriv).children('option').each(function(i){
	
			designString[i] = '<li><a href="#" rel="'+$(this).attr('value')+'">'+$(this).text()+'</a></li>';
	
			if($(this).attr('selected')!= undefined){
				currentLink = designString.slice(i,1);
				currentLink = '<a class="'+selectId+'-active" href="#">'+$(this).text()+'<span>&nbsp;</span></a>';
			}
		});
	
		designString = designString.toString().replace(/,/g,'');
		$(designString).appendTo($(selectIdPriv).next('ul'));
		$(currentLink).insertBefore($(selectIdPriv).next('ul'));
	
		$(selectIdPriv+'-list').hide();
		$(selectIdPriv+'-active').click(function(e){
			e.preventDefault();
			console.log($(this).css('left'));
			$(this).next('ul').toggle();
		});
	
		$(selectIdPriv+'-list li a').click(function(e){
			$(selectIdPriv+' option').removeAttr('selected');
			e.preventDefault();
			$(selectIdPriv+' option[value='+$(this).attr('rel')+']').attr('selected','selected');
			$(selectIdPriv+'-active').html($(this).text()+'<span>&nbsp;</span>');
			$(selectIdPriv+'-list').hide();
		});
	
		/*
		//TODO: HIDE UL on document click
		$(document).not($(selectIdPriv+'-list')).click(function(){
			$(selectIdPriv+'-list').hide();
		});
		$(document).not($(selectIdPriv+'-active')).click(function(){
			$(selectIdPriv+'-list').hide();
		});*/
	
	
	}
};

$(document).ready(function(){
	
});

$(document).ready(function(){

	$("select").selectbox();
	
	var p = Stores.getParams();


	if(p['store_region'] != undefined)
	{
	
		Stores.selectRegion(p['store_region']);
		Stores.selectCountry(p['store_country']);
		Stores.selectCity(p['store_city']);
	
	}
		
		
});

var Stores = {
	
	selectRegion : function(region) {
		
		$('#sel-country').html('');
		$('#sel-city').html('');
		$('#sel-submit').html('');
		
		if(region != '0')
		{
		
			var p = Stores.getParams();
			var sel = '';
		
			if(p['store_country'] != undefined)
			{
				sel = p['store_country'];
			}
		
			$.post("/",
	            'Func=stores.default.getCountries&region='+region+'&sel='+sel,
	            function(data) {
					$('#sel-country').html(data.store_countries);
					$("select").selectbox();
				},
				"json"
			);
			
		}
		
		
	},
	
	selectCountry : function(county) {
		
		$('#sel-city').html('');
		
		
		if(county != '0')
		{

			var p = Stores.getParams();
			var selcity = '';
		
			if(p['store_city'] != undefined)
			{
				selcity = p['store_city'];
			}
		
			$.post("/",
	            'Func=stores.default.getCity&county='+county+'&selcity='+selcity,
	            function(data) {
					$('#sel-city').html(data.store_cities);
					$("select").selectbox();
				},
			"json"
			);
		
		}
		
	},
	
	selectCity : function(city) {
		
		if(city != '0')
		{
			$('#sel-submit').html('<input class="submit" type="submit" value="Search" />');
		}
		else
		{
			$('#sel-submit').html('');
		}
		
	},
	
	getParams : function()
	{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
	}
	
	
	
};

var newsletter = {

	clearInput : function (item)
    {
        
		$(item).val('');
		
    },	
		
	subscribeForm : function()
	{
		$.post("/", 
			'Func=newsletter.subscribeForm', 
			function(data) {
				dialogHandler.popup(data);
			},
			"text"
		);
	},
    
	Subscribe : function(frm)
	{
		var formdata = frm;
		
		$.post("/", 
			'Func=newsletter.Subscribe&'+$(formdata).serialize(), function(data)
			{
				if (data.error)
	            {
					$('.newsletter-widget-error').show();
					$('.newsletter-error-text').html(data.msg);
	            }
	            else
	            {
	            	formdata.reset();
	            	$('.newsletter-widget-error').hide();
					$('.newsletter-widget-success').show();
					$('.newsletter-success-text').html(data.msg)
	            }
				
        }, "json");
	}
}
