var SPActions = {
	old_id:null,
	over:function (id, obj){
		if (typeof $('#action_link_'+id).data('qtip') == "undefined"){
			var text = $('#data_content_'+id).attr('innerHTML');
			if (typeof (text) != "undefined" && text.length>0){
				              
				var config = {
					content:{
						text:text
					},
					position: {						
						corner: {
							target: 'topLeft',
							tooltip: 'bottomMiddle'
						}
					},
					style: { 
					    border: {
					    	width: 7,
					        radius: 5,
					        color: '#9DC464'
					    },
					    tip:'bottomMiddle',
					    background:'#9DC464'    
					}
				};
				
				$('#action_link_'+id).qtip(config);
				
				var config = {
					content:{
						text:text
					},
					position: {
						target:$('#action_link_'+id),
						corner: {
							target: 'topLeft',
							tooltip: 'bottomMiddle'
						}
					},
					style: { 
					    border: {
					    	width: 7,
					        radius: 5,
					        color: '#9DC464'
					    },
					    tip:'bottomMiddle',
					    background:'#9DC464'    
					}
				};
				$('#action_info_ico_'+id).qtip(config);
			}
		}
		
		SPActions.old_id = id;		
	},
	out:function (id){
	},
	newsBlockShowHide:function (block){
		var o = document.getElementById('block_'+block);
		if (typeof o.list_actions_hidden == "undefined" || o.list_actions_hidden == false){
			o.list_actions_hidden = true;
			o.style.fontWeight = 'normal';
			$('tr.'+block).css('display', 'none');
			$('#ico_'+block).attr('src', '/img/sp_open.gif');
		}
		else{
			o.list_actions_hidden = false;
			o.style.fontWeight = 'bold';
			$('#ico_'+block).attr('src', '/img/sp_collapse.gif');
			try{
				$('tr.'+block).css('display', 'table-row');
			}catch(e){
				$('tr.'+block).css('display', 'block');
			}
		}
	}	
};

var SPReklamaIndex = {
	cur_id:null,
	bInProc:false,
	bCollFinished:false,
	bExpaFinished:false,
	over:function (id){
		if (id == SPReklamaIndex.cur_id || SPReklamaIndex.bInProc == true) return;
		SPReklamaIndex.bInProc = true;	
		SPReklamaIndex.bCollFinished = false;	
		SPReklamaIndex.bExpaFinished = false;	
		
		$('#rklm_block_head_'+SPReklamaIndex.cur_id).attr('className', 'rklm_acc_title_inactive');
		$('#rklm_block_head_'+id).attr('className', 'rklm_acc_title_active');
			
		$('#rklm_block_'+SPReklamaIndex.cur_id).slideToggle(500, function (){
			if (SPReklamaIndex.bExpaFinished == true){
				SPReklamaIndex.cur_id = id;
				SPReklamaIndex.bInProc = false;
			}
			else{
				SPReklamaIndex.bCollFinished = true;
			}
		});
		
		$('#rklm_block_'+id).slideToggle(500, function (){
			if (SPReklamaIndex.bCollFinished == true){
				SPReklamaIndex.cur_id = id;
				SPReklamaIndex.bInProc = false;
			}
			else{
				SPReklamaIndex.bExpaFinished = true;
			}
		});
	}
}

var SSearchIndex = {
	cur_id:"params",
	bInProc:false,
	bCollFinished:false,
	bExpaFinished:false,
	click:function (id){
		if (id == SSearchIndex.cur_id || SSearchIndex.bInProc == true) return;
		SSearchIndex.bInProc = true;	
		SSearchIndex.bCollFinished = false;	
		SSearchIndex.bExpaFinished = false;	
		
		$('#ss_'+SSearchIndex.cur_id+'_href').css('color', '#3C3D3D');
		$('#ss_'+id+'_href').css('color', '#665D50');
		
		$('#ss_'+SSearchIndex.cur_id+'_pic').css('backgroundImage', 'url(/img/plus.gif)');
		$('#ss_'+id+'_pic').css('backgroundImage', 'url(/img/minus.gif)');
		
		$('#ss_'+SSearchIndex.cur_id+'_content').slideToggle(100, function (){
			if (SSearchIndex.bExpaFinished == true){
				SSearchIndex.cur_id = id;
				SSearchIndex.bInProc = false;
			}
			else{
				SSearchIndex.bCollFinished = true;
			}
		});
		
		$('#ss_'+id+'_content').slideToggle(100, function (){
			if (SSearchIndex.bCollFinished == true){
				SSearchIndex.cur_id = id;
				SSearchIndex.bInProc = false;
			}
			else{
				SSearchIndex.bExpaFinished = true;
			}
		});
	}
}

function info(s){
	$('#info').css('display', 'block');
	$('#info').attr('innerHTML', "<pre>"+s+"</pre>");
}

function describe(o, bSub, iLevel){
	
	if (typeof iLevel == 'undefined') iLevel = 0;
	if (typeof bSub == 'undefined') bSub = true;
	
	var s = "";
	if (typeof (o)  == 'object'){
		for(k in o){
			for(i=0;i<iLevel;i++) s += "\t";
			if (typeof o[k] == "function"){
				s += k+":<span style='color:gray;'>[function ...]</span>\r\n";
			}
			else if (typeof o[k] == "object" && bSub == true){
				s += k+":{\r\n"+describe(o[k], bSub, iLevel+1)+"}\r\n";
			}
			else{
				s += k+":<span style='color:gray;'>"+o[k]+"</span>\r\n";
			}
		}
	}
	if (iLevel == 0) info(s);
	return s;
}

function scrollPlus(obj, val){
	var sl_cur = obj.scrollLeft(); 
	var sl_width = obj.attr('scrollWidth');
	var objWidth = obj.width();
	
	var nextValue = 0;
	
	if (sl_cur+val > sl_width-objWidth) nextValue = sl_width-objWidth;
	else if (sl_cur+val < 0) nextValue = 0;
	else nextValue = sl_cur+val;
	
	//obj.scrollLeft(nextValue);
	obj.animate({scrollLeft:nextValue}, 1000);
}

function scrollTopPlus(obj, val){
	var sl_cur = obj.scrollTop(); 
	var sl_width = obj.attr('scrollHeight');
	var objWidth = obj.height();
	
	var nextValue = 0;
	
	if (sl_cur+val > sl_width-objWidth) nextValue = sl_width-objWidth;
	else if (sl_cur+val < 0) nextValue = 0;
	else nextValue = sl_cur+val;
	
	//obj.scrollLeft(nextValue);
	obj.animate({scrollTop:nextValue}, 1000);
}

function news_producers_list(){
	if (document.getElementById('news_producers_list').style.display == 'none') {
		try{
			document.getElementById('news_producers_list').style.display = 'table-row'; 
		}
		catch(e){
			document.getElementById('news_producers_list').style.display = 'block'; 
		}
		document.getElementById('news_add_params_link').innerHTML = "Скрыть дополнительные параметры";
	}
	else {
		document.getElementById('news_add_params_link').innerHTML = "Дополнительные параметры";
		document.getElementById('news_producers_list').style.display = 'none';
	}
}

$(document).ready(function(){
	var images_gallery = $('.images_gallery');
	if (images_gallery.length > 0){
		$.each(images_gallery, function (index, images_gallery_next){
			var oBar = $('.images_gallery_panel', images_gallery_next);
			if (oBar.length > 0){
				var arrImages = $('a img', oBar[0]);
				if (arrImages.length > 0){
					var widthOfBar = 0;
					for(i=0;i<arrImages.length;i++){
						var img = $(arrImages[i]);
						$(img.parent()).bigPicture();
						widthOfBar += 147;
					}
					$(oBar[0]).width(widthOfBar);
					
				}
			}
		});
	}
	//
	$('#model_description').each(function (i, obj){
		var panel_height = $(obj).height();
		var txtHeight = $('#model_description #brand_short_description').height();
		if (txtHeight < panel_height) {
			$(obj).height(txtHeight);
		}
		var sh = $(obj).attr('scrollHeight');
		var h = $(obj).height();
		if (h < sh) {			
			$(obj).height(108);
			if (h > 108) {
				$('#md_btt_up').css("display","block");
				$('#md_btt_down').css("display","block");
			}
		}
	});
});

function sendfeedback(text){
	$.post('/sendfeedback.php',{text:text, url:window.location.href},function(data){
		alert("Ваше сообщение отправлено!");
		$('#test_msg_text').attr('value','');
	});
}

function newsLocation(sBase){
	var uri = "id="+sBase;
	if (document.getElementById('sp_specials_input').checked == true){
		uri+="&sp=1";
	}
	if (document.getElementById('news_brand_select').value != 0){
		uri+="&producer="+document.getElementById('news_brand_select').value;
	}
	window.location.href="/news/index.php?"+uri;
}

function autoReviewLocation(sBase,bVideo){
	var url = "/catalog/autoreview.php";
    if (bVideo == 1) {
        url = "/catalog/autovideos.php";
    }
	if (document.getElementById('news_brand_select').value != 0){
		url+="?producer="+document.getElementById('news_brand_select').value;
        url+="&model="+document.getElementById('news_models_select').value;
	}
	window.location.href=url;
}

function fb_like(){
    var s = "<iframe src=\"http://www.facebook.com/plugins/like.php?href="+document.location.href+"&amp;layout=button_count&amp;show_faces=false&amp;width=140&amp;action=recommend&amp;font=arial&amp;colorscheme=light&amp;height=21\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:140px; height:21px;\" allowTransparency=\"true\"></iframe>";
    document.write(s);
}

function loadBrandsToSelect(o, type, defaultID)
{   
    $.getJSON("/getBrands.php", {"type":type}, function(json) { 
        o.empty();
        o.append(createOption(0, "Не важно"));
        for (k in json) {
            opt = createOption(k, json[k]);
            if (k == defaultID) opt.attr("selected", "selected");
            o.append(opt);
        }
    });           
}

function createOption(k, v)
{
    option = $(document.createElement('option'));
    option.attr('value', k);
    option.html(v);
    return option;
}

function loadModelsToSelect(o, brandID, defaultID){
    $.getJSON("/getModels.php", {"brandID":brandID}, function(json) { 
        o.empty();
        o.append(createOption(0, "Не важно"));
        for (k in json) {
            opt = createOption(k, json[k]);
            if (k == defaultID) opt.attr("selected", "selected");
            o.append(opt);
        }
    });
}
