var rotator_current_item=0;
var preload_images=new Array();

function rotator_cycle() {
	if(rotator_current_item==rotate_text.length) rotator_current_item=0;
	$('#rotator_link')
		.html(rotate_text[rotator_current_item])
		.attr('href',rotate_url[rotator_current_item])
		.attr('target',rotate_target[rotator_current_item])
	rotator_current_item++;
	setTimeout("rotator_cycle()",5000);
}


var tip, tip_elem, tip_params, show_ptip;

function ptip(elem, params) {
	clearTimeout(show_ptip);
	tip_elem=elem;
	tip_params=params;

	show_ptip=setTimeout(ptip_go, 500);
	elem.onmouseout=ptip_hide;
}

function ptip_go() {
	elem=tip_elem;
	params=tip_params;

	/* 
	params[0] = type
	params[1] = width
	params[2] = preview
	params[3] = title
	params[4] = price
	params[5] = bottom_content
	*/

	html ='<div id="div_flv_preview">';
	html+='	<div class="flv_title">'+params[3]+'</div>';
	html+='	<div class="flv_price">'+params[4]+'</div>';
	html+='	<div class="clr"></div>';

	if(params[0]==1) { //type == 1 -> video
		ie=($.browser.msie==true ? '?time='+Number(new Date()) : '');

		html+='	<div id="flv_preview" style="width:400px; height:240px; overflow:hidden;">';
		html+='	<object width="400" height="240" type="application/x-shockwave-flash" bgcolor="#434343" quality="best" data="/includes/player.swf'+ie+'">';
		html+='		<param name="bgcolor" value="#434343" />';
		html+='		<param name="quality" value="best" />';
		html+='		<param name="movie" value="/includes/player.swf'+ie+'" />';
		html+='		<param name="flashvars" value="movie='+params[2]+'&autoload=on&autoplay=on" />';
		html+='	</object>';
		html+='	</div>';

//		html+=	AC_FL_GetContent('codebase','http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0','movie','/includes/player.swf?movie='+params[2]+'&autoload=on&autoplay=on'+ie,'width','400','height','240','src','top','quality','best','menu','false','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','/includes/player.swf?movie='+params[2]+'&autoload=on&autoplay=on'+ie);
	}

	html+='	'+params[5]+'<div class="clr"></div>';
	html+='</div>';

	tip.html(html);


	elw=$(elem).width();


	if(params[6]==0) { //normal tip
		x_off=3;
		x_off2=9;
		y_off=0;
		y_off2=15;

		if(elw>100) {
			x_off=1;
			x_off2=7;
			y_off=1;
			y_off2=16;
		}

		if(elw>200) {
			x_off=6;
			x_off2=14;
			y_off=3;
			y_off2=16;
		}
	}

	if(params[6]==1) { //in-cart
		x_off = x_off2 = 10;
		y_off=y_off2 = -137;
	}

	if(params[6]==2) { //account
		x_off = x_off2 = 20;
		y_off=y_off2 = -137;
	}


	offs=$(elem).offset();
	tleft=offs.left + elw + x_off;
	ttop=offs.top + y_off;


	if(params[6]==0) { //normal tip
		if(tleft+tip.width() > $(window).width()-12)		tleft=offs.left - tip.width() - x_off2;
		if(ttop+tip.height() > $(window).height()-12)	ttop=offs.top + elw - tip.height() + y_off2;
	}

	if(params[6]==2) { //account
		tleft=offs.left - tip.width() - x_off2;
	}


	tip.css({"width":params[1]+"px", "top":ttop+"px", "left":tleft+"px", "visibility":"visible"});
	elem.onmouseout=ptip_hide;
}

function ptip_hide() {
	clearTimeout(show_ptip);
	tip
		.html("")
		.css({"visibility":"hidden", "left":"-1000px"})
		.removeClass('normal')
}

function show_tip(elem,text) {
	tip.html(text);
	tip.addClass('normal');
	x_off=20;
	y_off=-32;

	elw=$(elem).width();
	elh=$(elem).height();
	offs=$(elem).offset();

	tleft=offs.left + x_off;
	ttop=offs.top - (elh) + y_off;

	tip.css({"width":"auto", "top":ttop+"px", "left":tleft+"px", "visibility":"visible"});
	elem.onmouseout=ptip_hide;
}

function show_tip2(elem,text) {
	tip.html(text);
	tip.addClass('normal');
	x_off=-2;
	y_off=-24;

	elw=$(elem).width();
	elh=$(elem).height();
	offs=$(elem).offset();

	tleft=offs.left + x_off;
	ttop=offs.top - (elh) + y_off;

	tip.css({"width":"auto", "top":ttop+"px", "left":tleft+"px", "visibility":"visible"});
	elem.onmouseout=ptip_hide;
}



/* ratings control */
jQuery.fn.ratings = function(stars, initialRating) {
return this.each(function() {
	if(!initialRating) initialRating=0;
	var containerElement=this;
	var container=jQuery(this);
	var starsCollection=Array();
	containerElement.rating = initialRating;

	for(var starIdx=0; starIdx<stars; starIdx++) {
		var starElement=document.createElement('span');
		var star=jQuery(starElement);
		starElement.rating = starIdx+1;
		star.attr('class','rating_star0');
		if(starIdx<initialRating) star.attr('class','rating_star1');
		container.append(star);
		starsCollection.push(star);

		star
			.click(function() {
				containerElement.rating=this.rating;
				$(containerElement).parent().find('input').val(this.rating);
			})
			.mouseenter(function() {
				for(var index=0; index<this.rating; index++) starsCollection[index].attr('class','rating_star1');
				for(var index=this.rating; index<stars; index++) starsCollection[index].attr('class','rating_star0');
			});

		container.mouseleave(function() {
			for(var index=0; index<containerElement.rating; index++) starsCollection[index].attr('class','rating_star1');
			for(var index=containerElement.rating; index<stars; index++) starsCollection[index].attr('class','rating_star0');
		});
	}
});
};


function expand_comment(elem, cls, reply_to) {
	$(elem).blur();

	if(reply_to==true) {
		header=$(elem).parent().parent().parent().find('div.header_add');
	} else {
		header=$(elem).parent();
	}


	if(reply_to==true) {
		to=$(elem).parent().find('td.left b').html();
		$('#'+cls+'_add_comment_cont').find('div.reply_cont').remove();
		$('#'+cls+'_add_comment_cont').find('#reply_to_id').val($(elem).parent().attr('id').replace('c',''));
		$('#'+cls+'_add_comment_cont').prepend('<div class="reply_cont">Reply: <b>'+to+'</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ <a href="javascript:void(0)" onclick="$(this).parent().parent().find(\'#reply_to_id\').val(0); $(this).parent().remove();">Remove Reply</a> ]</div>');
	} else {
		$('#'+cls+'_add_comment_cont').find('#reply_to_id').val(0);
	}


	if($('#'+cls+'_add_comment_cont').is(':visible')) {
	//hide
		if(reply_to==true) {
			window.location.hash='#add_comment';
			return false;
		}

		$('#'+cls+'_comments_cont').removeClass('prod_comments_cont');
		$('#'+cls+'_add_comment_cont').hide();
		header.removeClass('header_add_opened');
		$('#'+cls+'_add_comment_cont').find('div.reply_cont').remove();
	} else {
	//show
		header.addClass('header_add_opened');
		$('#'+cls+'_add_comment_cont').show();
		$('#'+cls+'_comments_cont').addClass(cls+'_comments_cont');
		window.location.hash='#add_comment';
	}
};


jQuery.fn.aForm = function(options) {
	options = options || {};
	
	$(this).bind("submit", function() {
		if(options.in_progress==1) return false;
		post_form(this);
		return false;
	});


	function post_form(forma) {
		options.in_progress=1;
		$(forma).parent().css('opacity',0.4);

		$.ajax({
			url: '/includes/forms.php?SID='+$('#SID').val()+'&form_name='+$(forma).attr('id'),
			data: $(forma).serialize(),
			type: 'post',
			dataType: 'script',
			cache: false,
			error: function(request) { alert('AJAX: Server connection error!'); },
			success: function(response) {
				options.in_progress=0;
				$(forma).parent().css('opacity',1);
			}
		});
	}
}


var prod_add_to_fav_process=false;
function prod_add_to_fav(elem,id,hover_add) {
	elem.blur();
	if(prod_add_to_fav_process==true) return false;

	prod_add_to_fav_process=true;
	if(hover_add!=true) {
		direction=(elem.className=='addtofav' ? 'add' : 'remove');
		elem.className=(elem.className=='addtofav' ? 'addtofav2' : 'addtofav');
	} else {
		direction=($(elem).parent().find('u').length==0 ? 'add' : 'remove');
		if(direction=='add') $(elem).parent().prepend('<u></u>');
	}

	$.ajax({
		url: '/includes/forms.php?SID='+$('#SID').val()+'&form_name=prod_add_to_fav_form',
		data: 'product_id='+id+'&direction='+direction,
		type: 'post',
		dataType: 'script',
		cache: false,
		error: function(request) { alert('AJAX: Server connection error!'); },
		success: function(response) {
			prod_add_to_fav_process=false;

			if(direction=='remove' || response=='0' || response=='-1') {
				if(hover_add!=true) {
					elem.className='addtofav';
				} else {
					if(direction=='remove' || response=='-1') $(elem).parent().find('u').remove();
				}
			}

			if(response=='-1') alert('You must be logged in to add this product to your favourites!');

			/* 
			alert(direction+' / '+response);
			if(direction=='add' && response=='1') {
				elem.className='addtofav2';
				alert('Product added to Your favourites!');
			}

			if(direction=='add' && response=='0') {
				elem.className='addtofav2';
				alert('Product already in Your favourites!');
			}

			if(direction=='remove' && response=='1') {
				elem.className='addtofav';
				alert('Product removed from Your favourites!');
			}

			if(direction=='remove' && response=='0') {
				elem.className='addtofav';
				alert('Product is not in Your favourites!');
			}
			*/
		}
	});
}


function cart(action,id,if_location) {
	if_location=(if_location==undefined ? '' : '&location='+if_location);

	$.ajax({
		url: '/includes/shopping_cart.php?SID='+$('#SID').val(),
		data: 'action='+action+'&id='+id+if_location,
		type: 'post',
		dataType: 'html',
		cache: false,
		error: function(request) { alert('AJAX: Server connection error!'); },
		success: function(response) { $("#cart").html(response); }
	});
}


function check_kupon() {
	var code=$.trim($('#kupon').val());
	if(code.length==0) return false;

	$.ajax({
		url: '/includes/forms.php?SID='+$('#SID').val()+'&form_name=discount_check',
		data: 'code='+code,
		type: 'post',
		dataType: 'script',
		cache: false,
		error: function(request) { alert('AJAX: Server connection error!'); },
		success: function(response) { }
	});
}


function change_nl_state(ch) {
	$("#finish_link").attr("href", "checkout,"+($("#mcredits").is(':checked')==true ? 'finish,mcredits' : 'finish')+($("#add_to_nl").is(':checked')==true ? ',nl':'')+".html");
}


function use_mcredits(ch) {
	$("#finish_link").attr("href", "checkout,"+($("#mcredits").is(':checked')==true ? 'finish,mcredits' : 'finish')+($("#add_to_nl").is(':checked')==true ? ',nl':'')+".html");
}


function save_cart(elem) {
	$(elem).blur();

	$.ajax({
		url: '/includes/shopping_cart_save.php?SID='+$('#SID').val(),
		type: 'get',
		dataType: 'script',
		cache: false,
		error: function(request) { alert('AJAX: Server connection error!'); },
		success: function(response) { }
	});
}


function blog_headnews() {
	$("#headnews_text").css('opacity',0.7);

	$.each($("#news_list a"),function(index,item){
		$(item).hover(function(){
			link=$(this);
			$("#news_list a").removeClass('hovered');
			$("#headnews_text").find('p').html('<b>'+link.find('span').html()+'</b>'+link.find('u').html());
			$("#headnews").css('background-image','url('+link.find('i').html()+')')
			$("#headnews").removeAttr('onclick');
			$("#headnews").bind('click', function() { window.location=link.attr('href'); });
		},function(){
			$(this).addClass('hovered');
		});
		preload_images[preload_images.length]=$(item).find('i').html();
	});
}


function check_categories_wraper() {
	if($("#categories_wraper").height()>45) {
		$("#categories_wraper").parent().parent().append('<a href="javascript:void(0)" onclick="this.blur(); expand_categories(); return false;" class="seeall" onmouseover="expand_categories(true)"></a>');
	}
}


var categories_expanded=false;
function expand_categories(hovered) {
	if(categories_expanded==true && hovered!=true) {
		$('#expand_categories').hide();
		categories_expanded=false;
	} else {
		$("#expand_categories")
			.html('<a class="close" href="javascript:void(0)" onclick="expand_categories()">[ Close ]</a><div class="title">All Categories</div><div class="list">'+$("#categories_wraper").html()+'</div>')
			.show();
		categories_expanded=true;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function(){
	tip=$("#prod_tip");
	ptip_hide();

	$("#top_menu a").click(function(){
		if($(this).attr('class')!='m6 dis') $(this).addClass('active');
	})

	if($("#qm0").length==1) {
		menu_create(0,false,0,300,true);
		qm_tree_init();
		item_sel=$("#qm0 a.selected");
		if(item_sel.length>0) {
			item_sel_off=$(item_sel).offset();
			menu_off=$("#qm0").offset();

			m_left=item_sel_off.left-menu_off.left;

			$(item_sel).css({"margin-left":-m_left+"px"})
			$(item_sel).css("padding-left",parseInt($(item_sel).css("padding-left"))+m_left+5+"px");
		}
	}

	if($("body.blog").length==0) $("#search_field").autocomplete("/includes/autocomplete_tag.php", { delay:250, width:212, max:16, scrollHeight:340 });

	if(window.location.hash=='#add_comment') $('#add_comment_link').click();

	if(preload_images.length>0) {
		for(var i=0; i<preload_images.length; i++) jQuery("<img>").attr("src", preload_images[i]);
	}
});
