﻿/*----------------------------------------------------------------------

	Project: ComBook

	Coded by Igor F. <me@guymedichi.net>

	Last edit: 26/08/2010

----------------------------------------------------------------------*/


(jQuery)(document).ready(function() {

	/* function leftWhiteSpace() {
		var HTMLobj = (jQuery)('body div.HTMLayer');
		var leftPadding = (jQuery)(HTMLobj).offset().left;
		var screenWidth = (jQuery)('body').width();// + leftPadding;

		if ( screenWidth > 1280 ) {
			leftPadding = ( screenWidth - 1280 > 250 ) ? 250 : screenWidth - 1280;
			screenWidth = screenWidth - leftPadding - 5;
		} else {
			leftPadding = 0;
		}

		(jQuery)(HTMLobj).css('paddingLeft' , leftPadding +'px').css('width' , screenWidth +'px');
	} */


	/* --[ OBJECTS ] -- */

	/* var catBest = {};

	catBest.init = function() {
		this.rootHolder = {}
		this.rootHolder.obj = (jQuery)('div.catalogSection');
		this.rootHolder.width = (jQuery)(this.rootHolder.obj).width() - 70;
		var globalPadding = ( (jQuery)('div.catalogueBestCE').is('.catalogueMainCE') ) ? 230 : 0;
		this.rootHolder.width = this.rootHolder.width - globalPadding;

		// удалить признаки врапперов
		for ( var i = 2; i <= 5 ; i++ ) {
			(jQuery)(this.rootHolder.obj).removeClass('with-'+ this.colNums +'-cols');
		}
		// уберем `в строчку`
		(jQuery)(this.rootHolder.obj).removeClass('items-inline');

		for ( var i = 2; i <= 5 ; i++ ) {
			this.colNums = i;
			var tempWidth = ( this.rootHolder.width - ( i - 1 ) * 30  ) / i;
			if ( tempWidth > 255 ) continue;
			if ( tempWidth < 245 ) {
				this.colNums = ( this.colNums > 2 ) ? i - 1 : 2;
				tempWidth = ( this.rootHolder.width - ( i - 1 ) * 30  ) / this.colNums;
				break;
			}
		}

		this.colWidth = ( tempWidth > 400 ) ? 400 : tempWidth;

		if ( this.colWidth > 255 ) {
			this.colNums = ( this.rootHolder.width * 1 + 30 * 1 ) / 280;
			this.colNums = this.colNums.toString().split('.');
			this.colNums = this.colNums[0];
			this.colWidth = 250;
		}
		

		//alert( this.colNums +' = '+ this.colWidth );
	}


	catBest.apply = function() {

		(jQuery)(this.rootHolder.obj).each(function() {
			var amount = (jQuery)(this).find('div.item-wrapper').size();
			if ( amount <= catBest.colNums ) {
				(jQuery)(this).addClass('items-inline');
			}
		});

		(jQuery)(this.rootHolder.obj).removeClass('with-2-cols').addClass('with-'+ this.colNums +'-cols');
		(jQuery)(this.rootHolder.obj).find('div.item-wrapper').css('width' , this.colWidth +'px');
	} */

	/* --[ EVENTS ] --- */

	//leftWhiteSpace();
	//catBest.init();
	//catBest.apply();


	(jQuery)('#popup-add2cart').click(function() {
		var cartURL = (jQuery)(this).attr('ref');
		window.location = cartURL;
	});

	
	(jQuery)('#popup-holder div.close').click(function() {
		(jQuery)('#pageContent div.newestBlock div.books div.holder').removeClass('holder-hovered').removeClass('holder-baloon');
		(jQuery)('#popup-holder').hide();
	});


	// --- catalogue navigation
	if ( (jQuery)("#pageContent div.siteNavigation div.section-active").size() < 1 ) {
		(jQuery)("#pageContent div.siteNavigation div.section:first").addClass('section-active');
	} else {
		if ( (jQuery)("#pageContent div.siteNavigation div.section-active ul li").size() < 1 ) {
			(jQuery)("#pageContent div.siteNavigation div.section-active").removeClass('section-active');
		}
	}


	// -- мегабаннер
	(jQuery)("#pageContent div.megaBanner div.cell").mouseenter(function() {
		this.currentX = (jQuery)(this).position().left;
		this.currentY = (jQuery)(this).position().top;
		(jQuery)(this).css( "left" , (this.currentX - 4) ).css( "top" , (this.currentY - 4) ).addClass( 'cell-hovered' );
	}).mouseleave(function() {
		(jQuery)(this).css( "left" , this.currentX ).css( "top" , this.currentY ).removeClass( 'cell-hovered' );
	});



	// -- навигация
	(jQuery)("#pageContent div.siteNavigation div.section").click(function() {
		if ( (jQuery)(this).is(".section-active") ) return;
		var newObj = this;
		var linkSuccess = true;
		(jQuery)("#pageContent div.siteNavigation div.section").each( function() {
			var oldObj = this;
			if ( newObj == oldObj && (jQuery)(oldObj).is(".section-active") ) return;
			if ( (jQuery)(newObj).find('ul li').size() > 0 ) {
				(jQuery)(oldObj).find("div.secondLevel").slideUp( 500, function() {
					(jQuery)(oldObj).removeClass("section-active");
					(jQuery)(newObj).addClass("section-active");
					(jQuery)(newObj).find("div.secondLevel").slideDown(500);
				});
				linkSuccess = false;
			}			
		});
		return linkSuccess;
	});
	
	
	// -- review vote stars

	(jQuery)('div.opinionList-add div.options div.option span.vote-star').mouseenter(function() {

		var arr = (jQuery)(this).attr('id').split('-');
		var opt = arr[2];
		var rate = arr[1];

		var code = (jQuery)(this).attr('rel');
		var currentCode = (jQuery)('#rate-' + rate ).attr('value');

		if ( 1 * currentCode < code * 1 ) {
			var obj = (jQuery)(this).parent().get(0);
			(jQuery)(obj).find('span.vote-star:lt('+ (opt) +')').addClass('vote-star-on');
		}

	}).mouseleave(function() {

		var arr = (jQuery)(this).attr('id').split('-');
		var opt = arr[2];
		var rate = arr[1];

		var code = (jQuery)(this).attr('rel');
		var currentCode = (jQuery)('#rate-' + rate ).attr('value');

		var obj = (jQuery)(this).parent().get(0);
		(jQuery)(obj).find('span.vote-star:lt('+ (opt) +')').each(function() {
			if ( (jQuery)(this).is('vote-star-set') ) {
			} else {
				(jQuery)(this).removeClass('vote-star-on');
			}
		});
	
	}).click(function() {

		var arr = (jQuery)(this).attr('id').split('-');
		var opt = arr[2];
		var rate = arr[1];

		var code = (jQuery)(this).attr('rel');
		(jQuery)('#rate-' + rate ).attr( 'value' , code );

		var obj = (jQuery)(this).parent().get(0);

		(jQuery)(obj).find('span.vote-star').removeClass('vote-star-set').removeClass('vote-star-on');
		(jQuery)(obj).find('span.vote-star:lt('+ (opt) +')').addClass('vote-star-set');

	});


	// -- lightbox
	if ( (jQuery)("#book-flip-content a[rel^='bookPreview']").size() < 1 ) {
		(jQuery)('#book-flip-anchor').hide();
		(jQuery)('div.bookDetails div.sidebar ul.bookActions li.flip').hide();
	} else {

		(jQuery)("#book-flip-content a[rel^='bookPreview']").prettyPhoto({
			theme: 'pp_sbubnom'
		});
	}

	
	// -- flip
	(jQuery)('#book-flip-anchor').click(function() {
		(jQuery)('#book-flip-content a:first').click();
	});
	
	(jQuery)('div.bookDetails div.sidebar ul.bookActions li.flip').click(function() {
		(jQuery)('#book-flip-content a:first').click();
	});


	// shipping

	if ( (jQuery)('#form-adress-new').is('.fields-hidden') ) {
		hideShippingAddressForm();
	}


	function hideShippingAddressForm() {

		(jQuery)('#form-adress-new div.saved-address').hide();
		(jQuery)('#form-adress-new').addClass('fields-hidden');
		(jQuery)('#form-adress-new div.field input.sb_required , #form-adress-new div.field select.sb_required').each(function() {
			(jQuery)(this).removeClass('required-entry').removeClass('validate-select');
		});

		(jQuery)('input[name=no_address]').attr( 'disabled' , true );

	}


	function showShippingAddressForm() {
		(jQuery)('#form-adress-new div.saved-address').show();
		(jQuery)('#form-adress-new').removeClass('fields-hidden');
		(jQuery)('#form-adress-new div.field input.sb_required , #form-adress-new div.field select.sb_required').each(function() {
			(jQuery)(this).addClass('required-entry');
		});
		(jQuery)('input[name=no_address]').attr( 'disabled' , false );
	}


	// переключение сохраненных адресов
	(jQuery)('#address-address-select').change(function() {
		if ( (jQuery)(this).attr('value') == '' ) {
			showShippingAddressForm();
		} else {
			hideShippingAddressForm();
			(jQuery)('#form-adress-new div.saved-address').show();
		}
	});

	// по умолчанию `курьер`
	// см. [SKIN]/js/opcheckout.js [312]
	(jQuery)('#s_method_citycourier_citycourier').attr( 'checked' , 'checked' );
	(jQuery)('input[name=no_address]').attr( 'disabled' , false );
	(jQuery)('#form-adress-new div.saved-address').show();

	(jQuery)('input[name=shipping_method]').live( 'click' , function() {

		var addressID = (jQuery)('#address-address-select').attr('value');

		if ( (jQuery)(this).attr( 'id' ) == 's_method_freeshipping_freeshipping' ) {
			hideShippingAddressForm();
			return;
		}
		if ( (jQuery)(this).attr( 'id' ) == 's_method_flatrate_flatrate' ) {
			hideShippingAddressForm();
			return;
		}

		if ( addressID ) {
			if ( (jQuery)(this).attr( 'id' ) == 's_method_citycourier_citycourier' ) {
				hideShippingAddressForm();
				(jQuery)('#form-adress-new div.saved-address').show();
				return;
			}
			if ( (jQuery)(this).attr( 'id' ) == 's_method_ems_ems' ) {
				hideShippingAddressForm();
				(jQuery)('#form-adress-new div.saved-address').show();
				return;
			}
		} else { // id is present
			if ( (jQuery)(this).attr( 'id' ) == 's_method_citycourier_citycourier' ) {
				showShippingAddressForm();
				return;
			}
			if ( (jQuery)(this).attr( 'id' ) == 's_method_ems_ems' ) {
				showShippingAddressForm();
				return;
			}
		}

	});
	
	// -- password reminder
	
	(jQuery)('#forgotten-password').click(function() {
		(jQuery)('div.password-remind').show();
		return false;
	});
	
	
	
	
	
	
	
	
	/* (jQuery)(window).resize(function() {
		//leftWhiteSpace();
		//catBest.init();
		//catBest.apply();
	}); */

});

