var defaults = new Object;
var gaframe;

function flashSetGa(path) {
	gaSetFrame(path, true);
}

function gaGetFrame() {
	if (!gaframe) {
		gaframe = document.createElement('iframe'); gaframe.width = 0; gaframe.height = 0; gaframe.style.border = "none";
		document.body.appendChild(gaframe)
	}
	
	return gaframe;
}

function gaSetFrame(frameSrc, withParam) {

	if (withParam) {
		frameSrc += ((frameSrc.indexOf("?")==-1)?"?":"&") + "ga=Y";
	}

	gaGetFrame().src = frameSrc
}

$(document).ready(function() {



	defaults.fancybox = {
		overlayColor		:	'#FFF',
		padding				:	0,
		margin				:	0,
		overlayOpacity		:	0.8,
		titleShow			:	false,
//		autoDimensions		:	false,
		autoScale			:	false,
		scrolling: 'none',
		onComplete: function() {			
			
			var p_f = $('#press-fansy');
			var f_w = $('#fancybox-wrap');
			
			p_f.css('height', f_w.height()-26 +'px');			
			p_f.parent().parent().css('height', f_w.height()+20 +'px');
						
			if($('table.pp-viewer-i:visible .b-txt').height()) {
				var border = 3;
				if(f_w.height() < $('table.pp-viewer-i:visible .b-txt').height()+17+25+border) {
					
					f_w.css('height', $('table.pp-viewer-i:visible .b-txt').height()+17+25+ border+'px');
				}
			}	
				
					
			if(  p_f.css('display') != 'block' && f_w.css('display') == 'block' && f_w.height()!=$('#fancybox-outer', f_w).height()) {				
				$('#fancybox-outer', f_w).css('height', $('#fancybox-wrap').height()+'px');
//				/$('#fancybox-outer').removeAttr('style');
			}
			
			if (this.type == "ajax") {
				gaSetFrame(this.href, true);
			}
			else if (this.type == "inline" && this.orig[0].rel) {
				if (this.orig[0].rel!="")
					gaSetFrame(this.orig[0].rel, true);
			}

                        if($('#discount', f_w).length > 0)
                        {
                                f_w.css('left', '50px');
                        }
		}	
	}
			
	$('a.fancybox').fancybox(defaults.fancybox);

	if(document.location.href.indexOf("#advise_")!=-1) 
	{	
		var h = $('[href="'+window.location.hash+'"]');
		console.log(window.location.hash);
		console.log(h);
		$('[href="'+window.location.hash+'"]').click();
	}
	
	function showRules() {		
		$('#rules').click();		
	}
	
	if($('.init-catnavauto').length > 0) {
		startauto();
	}
	if($('.b-mnmiddle').length > 0) {
		var $mnmid = $('.b-mnmiddle');
		$('.b-mnmiddle .link-show').click(function() {
			var rClick = window.document.pdrswf;
			
			if (rClick) {
				if($mnmid.hasClass('active-html')) {
					showFlash();
					rClick.expand();
					return false;
				}
				else {
					rClick.collapse();
				}
			}
			return false;
		});
	
		$(".b-ph-scroll ul",$mnmid).each(function(){
			var $this = $(this);
			if($('li',$this).length > 1) {
				$this.cycle({
					next: $this.parent().next('.b-ars').find('.r-ar'),
					prev: $this.parent().next('.b-ars').find('.l-ar'),
					fx:'scrollHorz',
					speed:500,
					before:function(currSlideElement, nextSlideElement, options, forwardFlag) {
				
						showops(currSlideElement, nextSlideElement, $this)
					},
				
					timeout:0
				});
			}
		});
	
		
	}
	defaults.datepicker = {
		changeMonth: true,
		changeYear: true,
		yearRange: '1945:c',
		showMonthAfterYear: true,
		beforeShow: function(input, inst) { inst.dpDiv.css({marginTop: -input.offsetHeight + 'px', marginLeft: input.offsetWidth + 'px'})}
	}
	if($('.b-forms').length > 0) {

		$("form.b-forms").jqTransform();
		$('.b-forms .date-pick').datepicker(defaults.datepicker);
	}
	var sideform = $('.b-sideform');
	if (sideform.length > 0) {
		sideform.jqTransform();
		if (sideform.find('#property_city').val() != "Москва"){
			sideform.find('#property_metro').siblings('div').find('span').css('color','#cccccc');
		}
		sideform.find('#property_city').change(function(){
			var color = '#000000';
			if ($(this).val() != "Москва")	color = '#cccccc';
			sideform.find('#property_metro').siblings('div').find('span').css('color',color);
		});
	}

});
	



function startauto() {
	var animauto = 0, idx = 0;
	var $inauto = $('.init-catnavauto');
	var totsz = $('li',$inauto).length;
	
	function showcat() {
		if(idx < totsz) {
			$('li a',$inauto).removeClass('hr').eq(idx).addClass('hr');
			idx++
		}
		else {
			$('li a',$inauto).eq(idx-1).removeClass('hr');
			clearInterval(animauto);
		}
	}
	
	$inauto.bind('mouseenter', function(event) {
		$('li a',$inauto).removeClass('hr');
		clearInterval(animauto);
	});

	animauto = setInterval(showcat,1000)
}

function showops(currSlideElement, nextSlideElement, cont) {
	var $t = cont;
	var nextSlide = $('li',$t).index(nextSlideElement);
	var currSlide = $('li',$t).index(currSlideElement);
	var $contblock = $t.parent().parent().find('.b-txt');
	if(nextSlideElement != currSlideElement) {
		

		$('li',$contblock).eq(nextSlide).css({position:'absolute',top:0,opacity:0}).animate({
			opacity:1
		}, 700).css({"z-index":'1000'});  		
		
		$('li',$contblock).eq(currSlide).animate({
			opacity:0
		}, 700).css({"z-index":'-1000'});
		
	} else {
		$('li',$contblock).css({position:'absolute',top:0,opacity:0}).eq(currSlide).css('opacity','1').css({"z-index":'1000'});
	}
}

function hideFlash() {
	$('.b-mnmiddle').addClass('active-html');
}
function showFlash() {
	$('.b-mnmiddle').removeClass('active-html');
}
function mainFlashResultFn(result) {
	if (result.success === false) {
		// no flash
		$(".b-flashreq").addClass("isNoFlash");
		$(".link-show", ".b-mnmiddle").click(function(){
			window.open("http://get.adobe.com/flashplayer/");
		})
		hideFlash();
	}
	else {
		$(".b-flashreq").addClass("isFlash");
	}
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
