var fancyDefaultPhotoParams = {
	titleShow	: false,		
	'overlayShow'		: true,
	'overlayColor'	: '#fff',
	'padding'		: 0,
	'margin'		: 100,
	'speedIn'		: 600,
	'overlayOpacity'	: 0.6,
	'speedOut'		: 500,
	
	'transitionIn'	: 'elastic',
	'transitionOut'	: 'elastic',
	'easingIn'		: 'easeOutBack',
	'easingOut'		: 'easeInBack',

	onComplete: function() {
		if(typeof(Cufon == 'function'))
			Cufon.refresh();
	}			
};

var fancyDefaultVideoParams = {
		titleShow	: false,		
		'hideOnContentClick': false,
		'overlayShow'		: true,
		'overlayColor'	: '#fff',
		'padding'		: 0,
		'margin'		: 0,
		
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'type'			: 'swf',
		'swf'			: {
			'wmode'			: 'transparent',
			'allowfullscreen'	: 'true'
		}
		
	};

var fancyDefaultShareParams = $.extend({}, {
	
		shareUrl	: '/bitrix/tools/media.share.php',
		shareFormatId: function(id) {
			return id.replace('link_', '');
		}		
	},
	fancyDefaultPhotoParams
);

function getCharCode(ev) {

	var charCode = 0;
	if (ev.charCode) charCode = ev.charCode;
	else if (ev.keyCode) charCode = ev.keyCode;
	else if (ev.which) charCode = ev.which;

	return charCode;
}


$(document).ready(function() {
	
	var hash = null;
	if (document.location.hash != "") {
		hashTmp = document.location.hash.toString().substring(1);
		if (!isNaN(hashTmp))
			hash = hashTmp;
	}

	if (null != hash) {
		var el = $("#link_" + hash);
		if(el.length > 0)
			setTimeout(function() { el.trigger("click"); }, 300);
	}

	if(typeof($.fn.fancyboxShare) == 'function') {
		$(".shareit").fancyboxShare(fancyDefaultShareParams);
		$(".popup_video").fancybox(fancyDefaultVideoParams);
		$(".popup_image").fancybox(fancyDefaultPhotoParams);
	}
	
	$(".styled_sbm_btn").bind("mousedown", function() {
		$(this).addClass("pressed");
	});
	
	$(document).bind("mouseup", function() {
		$(".styled_sbm_btn.pressed").removeClass("pressed");
	});	

	$("#top2 .menu span, .models_menu a")
		.hover(function(){
			$(this).addClass("showhidden").removeClass("showshow");
		},
		function(){
			$(this).addClass("showshow").removeClass("showhidden");
		});

	if(typeof Cufon != "undefined")
	{
		Cufon.replace('.sifr-content-header, .sifr-dealer-string', {letterSpacing: '0.5px', fontFamily: 'Neo Sans Pro'});
		Cufon.replace('.sifr-content-header-bold', {letterSpacing: '0.5px', fontFamily: 'Neo Sans Pro Medium'});
		
		Cufon.replace('.neo_sans_medium', { fontFamily: 'Neo Sans Pro Medium'});
		Cufon.replace('.neo_sans_light', { fontFamily: 'Neo Sans Pro Light'});
		Cufon.replace('.neo_sans_regular', { fontFamily: 'Neo Sans Pro'});		
	}

	if (!(document.all && !window.opera && !window.XMLHttpRequest && $.browser.msie)) {
		$("#top2 .links > a, #top1 .links > a, #search_form > a").hover(
			function (){
				$("img", this)
					.attr("tmp", $("img", this).attr("src"))
					.attr("src", $("img", this).attr("hsrc"));
			},
			function (){
				$("img", this)
					.attr("src", $("img", this).attr("tmp"));
			}
		)
	}


	$(".ddown a").hover(
		function (){
			$("img", this)
				.attr("tmp", $("img", this).attr("src"))
				.attr("src", $("img", this).attr("hsrc"));
		},
		function (){
			$("img", this)
				.attr("src", $("img", this).attr("tmp"));
		}
	)


});

function open_win(pg){
	var res = window.open(pg, "", "width=550,height=350,scrollbars=yes");
	return false;
}

function checkboxCheck(obj){

	if ($(obj).parent().attr("class")=="checkbox1" || $(obj).parent().attr("class")=="checkbox2") {

		if ($(obj).is(":checked")) setClass = "checkbox2";
		else setClass = "checkbox1";

		$(obj).parent().attr("class", setClass);
	}

	if ($(obj).parent().attr("class")=="checkbox1_yellow" || $(obj).parent().attr("class")=="checkbox2_yellow") {

		if ($(obj).is(":checked")) setClass = "checkbox2_yellow";
		else setClass = "checkbox1_yellow";

		$(obj).parent().attr("class", setClass);
	}
}

$(document).ready(function(){
	$("span > input[type=checkbox]").each(function(){

	$(this).click(function(){
								if (this.checked)
									this.checked = "checked";
								else
									this.checked = "";
								checkboxCheck(this);
							});
	checkboxCheck(this);
	})
});

function debug()
{
        return document.location.href.indexOf('debug') > -1;
}
function p(s)
{
	if(debug())
		console.log(s);
}

//Grayscale w canvas method
var GrayscaleImage = {
		
		getSrc: function(src) {
			var canvas = document.createElement('canvas');
			var ctx = canvas.getContext('2d');
			var imgObj = new Image();
			imgObj.src = src;
			canvas.width = imgObj.width;
			canvas.height = imgObj.height; 
			ctx.drawImage(imgObj, 0, 0); 
			var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
			for(var y = 0; y < imgPixels.height; y++){
				for(var x = 0; x < imgPixels.width; x++){
					var i = (y * 4) * imgPixels.width + x * 4;
					var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
					imgPixels.data[i] = avg; 
					imgPixels.data[i + 1] = avg; 
					imgPixels.data[i + 2] = avg;
				}
			}
			ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
			return canvas.toDataURL();			
		},
		
		make: function(img) {
			var el = $(img);
			el.css({"position":"absolute"}).wrap("<div class='grayscale_img_wrapper'>").clone().addClass("grayscale_img").insertAfter(el).queue(function() {
				var el = $(this);
				el.parent().css({"width":this.width,"height":this.height});
				el.dequeue();
			});
			$(img).attr("src", this.getSrc(img.src));			
		}
};
