/**
 * @author .DeV!L @ 2009
 */

var h, h2, h3, bill_country, bill_state;
var feats = new Array();
var notify = new Array();

$(function(){
	$('#tabs > li').click(function(){
		$('#tabs > li').removeClass('active');
		$(this).addClass('active');
		$('.tab_panel').hide();
		$('#panel_'+$(this).attr('name')).show();
		return false;
	});
	
	$('#info_tabs > li').click(function(){
		$('#info_tabs > li').removeClass('active');
		$(this).addClass('active');
		$('.info_body').hide();
		$('#panel_'+$(this).attr('name')).show();
		return false;
	});
	
	/**
	 * Browse Menu
	 */
	$('#menu_browse').hover(function(){
		clearTimeout(h);
		$('#browse_submenu').show();
		$(this).addClass('hl');
	}, function(){
		h = setTimeout(function(){
			$('#browse_submenu').hide();
			$('#menu_browse').removeClass('hl');
		}, 200);
	});
	
	$('#browse_submenu > ul').hover(function(){
		clearTimeout(h);
	}, function(){
		h = setTimeout(function(){
			$('#browse_submenu').hide();
			$('#menu_browse').removeClass('hl');
		}, 200);
	});
	
	/**
	 * cat menu
	 */
	$('#menu_cats').hover(function(){
		clearTimeout(h2);
		$('#cats_submenu').show();
		$(this).addClass('hl');
	}, function(){
		h2 = setTimeout(function(){
			$('#cats_submenu').hide();
			$('#menu_cats').removeClass('hl');
		}, 200);
	});
	
	$('#cats_submenu > ul').hover(function(){
		clearTimeout(h2);
	}, function(){
		h2 = setTimeout(function(){
			$('#cats_submenu').hide();
			$('#menu_cats').removeClass('hl');
		}, 200);
	});
	
	/**
	 * money menu
	 */
	$('#menu_money').hover(function(){
		clearTimeout(h3);
		$('#money_submenu').show();
		$(this).addClass('hl');
	}, function(){
		h3 = setTimeout(function(){
			$('#money_submenu').hide();
			$('#menu_money').removeClass('hl');
		}, 200);
	});
	
	$('#money_submenu > ul').hover(function(){
		clearTimeout(h3);
	}, function(){
		h3 = setTimeout(function(){
			$('#money_submenu').hide();
			$('#menu_money').removeClass('hl');
		}, 200);
	});
	/**
	 * --------------
	 */
	
	$('#log_username').focus(function(){
		if ($(this).val() == 'username'){
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == ''){
			$(this).val('username');
		}
	});
	
	$('#pass_holder').focus(function(){
		$(this).hide();
		$('#log_password').show().focus();
	});
	
	$('#pass').blur(function(){
		if ($(this).val() == ""){
			$(this).hide();
			$('#pass_holder').show();
		}
	});
	
	$('.settings_block .block_title').click(function(){
		var body = $(this).next();
		if (!body.hasClass('active')){
			$('.settings_block .active').slideUp().removeClass('active');
			body.slideDown().addClass('active');			
		}
	});
	
	$('input[name="file_type"]').click(function(){
		switch($(this).val()){
			case '0':
				$('#type_file').show();
				$('#type_url').hide();
				break;
				
			case '1':
				$('#type_file').hide();
				$('#type_url').show();
				break;
		}
	});
	
	//view toggler
	$('#change_view_list').click(function(){
		$.cookie('view_type', 'list', {'path': '/', expires: 356});
		top.location = top.location;
		return false;
	});
	
	$('#change_view_grid').click(function(){
		$.cookie('view_type', 'grid', {'path': '/', expires: 356});
		top.location = top.location;
		return false;
	});
	
	//order_by
	$('#order_by').change(function(){
		$.cookie('order_by', $(this).val(), {'path': '/', expires: 356});
		top.location = top.location;
		return false;
	});
	
	//cats list
	$('.cat_list > li').click(function(){
		var n = $(this).next();
		if ($(this).hasClass('cat_list_par_open')){
			return;
		}
		if (n.hasClass('cat_list_sub')){
			if (n.is(':hidden')){
				n.slideDown();
				$(this).css('backgroundImage', 'url(../images/arr_down.gif)');				
			}else{
				n.slideUp();
				$(this).css('backgroundImage', 'url(../images/arr.gif)');
			}
		}
	});
	//main page rolls
	$('#rolls > .rHead > h4').click(function(){
		var b = $(this).parent().next();
		if (b.is(':hidden')){
			$('.rBody').slideUp();
			b.slideDown();
		}
	});
	
	//add to cart
	$('#btn_cart').click(function(){
		if ($(this).hasClass('do_auth'))
		{
			$.gritter.add({
				title: 'Shopping Cart',
				text: 'Log in or Sign up, Please!',
				time: 4000
			});
			return false;
		}
		var id = $(this).attr('prod');
		$.get(
			'/cart/add/'+id,
			{},
			function(data){
				switch(data.result) {
					case 0:
						switch (data.value) {
							case 0:
								//alert('Wrong Product!');
								$.gritter.add({
									title: 'Shopping Cart',
									text: 'Wrong Product!',
									time: 4000
								});
								break;
							case 1:
								//alert('Product is already in cart!');
								$.gritter.add({
									title: 'Shopping Cart',
									text: 'Product is already in cart!',
									time: 4000
								});
								break;
							case 2:
								$.gritter.add({
									title: 'Shopping Cart',
									text: 'You already bought this product!',
									time: 4000
								});
								break;
						}
					    
						break;
					case 1:
						$('#sh_cart_link').text('Shopping Cart ('+parseInt(data.value)+')');
						$.gritter.add({
							title: 'Shopping Cart',
							text: 'Product successfully added in your cart!',
							image: '/components/'+id+'/images/thumb.jpg',
							time: 4000
						});
						break;
				}
			},
			'json'
		);
		
		return false;
	});
	
	//notify
	if (notify.length > 0){
		for(i = 0; i < notify.length; i++){
			$.gritter.add({
				title: notify[0].title,
				text: notify[0].text,
				time: 4000
			});
		}
	}
	
	//feats
	if (feats.length > 0){
		for (i = 0; i < feats.length; i++){
			$('#feat_img > a').append($('<img src="'+feats[i].img+'" border="0" width="330" id="feat_img_'+i+'" />').css('opacity', 0));
		}
		var feat_ind = 0;
		$('#feat_img_0').css('opacity', 1);
		$('#feat_img > a').attr('href', feats[0].url);
		$('#feat_price').html('$'+feats[0].price);
		$('#feat_name > a').attr('href', feats[0].url).html(feats[0].name);
		setInterval(function(){
			if (feat_ind >= feats.length-1){
				prev_ind = feats.length-1;
				feat_ind = 0;
			}else{
				prev_ind = feat_ind++;
			}
			
			$('#feat_img_'+prev_ind).animate({
				'opacity': 0
			}, 1000);
			$('#feat_img_'+feat_ind).animate({
				'opacity': 1
			}, 1000);
			
			$('#feat_price').html('$'+feats[feat_ind].price);
			$('#feat_img > a').attr('href', feats[feat_ind].url);
			$('#feat_name > a').attr('href', feats[feat_ind].url).html(feats[feat_ind].name);
			
		}, 3000);
	}
	
	$('#stat_view').click(function(){
		top.location = '/earnings/'+eDate.year+((eDate.month>0)?'-'+eDate.month:'');
		return false;
	});
	
	// -- billing form
	if (bill_country != null && bill_country == ''){
		$('#country option[value="UNITED STATES"]').attr('selected', 'selected');
		$('#state_us').show();
	}else{
		$('#country option[value="'+bill_country+'"]').attr('selected', 'selected');
		switch(bill_country){
			case 'UNITED STATES':
				$('#state_us').show().attr('name', 'state');
				setSelect('state_us', bill_state);
				break;
			case 'CANADA':
				$('#state_ca').show().attr('name', 'state');
				setSelect('state_ca', bill_state);
				break;
			default: 
				$('#state_other').show().attr('name', 'state').val(bill_state);
				break;
		}
	}
	$('#country').change(function(){
		var item = $('option:selected', $(this));
		$('#phone').val('+'+item.attr('phone'));
		switch(item.val()){
			case 'UNITED STATES':
				$('#state_us').show().attr('name', 'state');
				$('#state_other').hide().attr('name', '');
				$('#state_ca').hide().attr('name', '');
				break;
			case 'CANADA':
				$('#state_ca').show().attr('name', 'state');
				$('#state_us').hide().attr('name', '');
				$('#state_other').hide().attr('name', '');
				break;
			default: 
				$('#state_other').show().attr('name', 'state');
				$('#state_us').hide().attr('name', '');
				$('#state_ca').hide().attr('name', '');
				break;
		}
	});
 });
 
function setSelect(id, value)
{
 	$('#'+id+' option[value="'+value+'"]').attr('selected', 'selected');
}

function setRadio(name, value)
{
	$('input[name="'+name+'"][value="'+value+'"]').attr('checked', 'checked');
}

function file_type(val)
{
	setRadio('file_type', val);
	switch(val){
		case '0':
			$('#type_file').show();
			$('#type_url').hide();		
			break;
		case '1':
			$('#type_file').hide();
			$('#type_url').show();
			break;
	}
}

function echo(v)
{
	console.log(v);
}
