$(document).ready(function(){
	
var productNav = $('#products-block');
var productBut = $('#products-link');
$('html').removeClass('no_js');



var colorboxSettings = {opacity:0.9,close:'',maxWidth:'100%',initialWidth:200,initialHeight:200}

productNav.hide();
productBut.click(function(){
productNav.slideToggle('fast');
$(this).toggleClass('active');
return false;
})

//homepage products nav
$('#hp_grid').find('li').css({cursor:'pointer'}).click(function(){ 
	var url = $(this).find('a').attr('href');
	window.location = url;
})

$('#pg_dclink').find('#product_boxes').find('a').click( function(){ scrollTo($(this).attr('href'));return false; } )

//add support for IE6 -------------- //

if( getClient() == 'IE6' ) {
	$('input[type=text]').addClass('text');
}

// gridConnect bar
hoverFade($('#gridconnectBar'));
$('#gridconnectBar').click(function(){window.location='http://www.es-pv.co.uk/';});

//colorbox triggers ------------------
var cboxPages = $('#pg_eplex,#pg_lightnet5,#pg_ac_changeover,#pg_hd_link_box');
if( cboxPages.length ) { cboxPages.find('a.cbox,a.cb').colorbox(colorboxSettings); }


$('#faq_questions').find('a').click(function(){ scrollTo($(this).attr('href'));return false; });


});// end ready

function email(props){
	var data = {}, f = $('form');
	if( validate(f.find('input.req')) ){
		f.find('input, textarea').not(':input[type=submit]').each(function(){
			var t = $(this);
			data[t.attr('name')] = t.val();
			t.not(':[type=hidden]').val('');
		}); 
		$.colorbox({href:'../_inc/ssi/mail.php',data:data,width:400,initialWidth:100,initialHeight:93,close:''});
	}
}

Slider = function(opts){
	var defaults = {
		scroller: 'div.scroll',
		next: 'a.next',
		prev: 'a.prev',
		items: 'li',
		speed: 200,
		row:1,
		itemW: false,
		pagingWrap: []
	}
	var o = jQuery.extend({},defaults,opts);
	var t = this;
	var pagingWrap = opts.pagingWrap || 'div.paging';
	
	this.index = 0;
	this.wrap = jQuery(o.wrap);
	this.scroller = this.wrap.find(o.scroller);
	this.mask = this.wrap.find('div.mask');
	this.items = this.scroller.find(o.items);
	this.itemW = (function(){ if(!o.itemW) { return t.items.outerWidth(true)*o.row } else { return o.itemW*o.row } })();
	this.btn_next = this.wrap.find(o.next);
	this.btn_prev = this.wrap.find(o.prev);
	this.row = opts.row || 1;
	this.total = Math.ceil(this.items.length / this.row);
	this.speed = opts.speed || 200;
	this.callback = opts.callback || function(){};
	this.pagingNav = this.wrap.find(pagingWrap);
	
	(function(t){
		t.mask.height(t.items.outerHeight(true));
		t.scroller.width( t.items.length * t.itemW );
		t.items.css({float:'left'});
		t.btn_next.click(function(){t.next();return false;});
		t.btn_prev.click(function(){t.prev();return false;});
		if( t.pagingNav.length ) { t.initPaging(); }
		t.setBtnStatus();
	})(this); 
	return t;
}
Slider.prototype = {
	getIndex:function (){
		if( this.index <=0 ){ this.index = 0 }
		else if( this.index >= this.total - 1 ) { this.index = this.total -1 }
		return this.index;
	},
	next:function(){
		this.index++;this.goTo(this.getIndex());
	},
	prev:function(){
		this.index--;this.goTo(this.getIndex());
	},
	goTo:function(n,callBack){
		var t = this, cb = callBack || function(){};
		this.scroller.animate({left: -(this.itemW * n)},this.speed,'swing',function(){ t.setActive(); t.callback();} );
		this.index = parseInt(n);
		this.setBtnStatus();
		return this;
	},
	initPaging: function(){
		if(this.total == 1){ this.pagingNav.html(''); return false;}
		var pi = '<ul class="sliderPaging">',t=this;
		if(this.total > 1){
			for(var i=0; i<this.total; i++){ pi+='<li><a href="#"><span>'+parseInt(i)+'</span></a></li>';}
			this.pagingNav.html(pi+'</ul>');
			this.pagingLinks = this.pagingNav.find('a');
			this.pagingLinks.click(function(){ t.goTo( $(this).find('span').text() ); return false; })
			this.setActive();
		}
	},
	setActive: function(){
		var pl = this.pagingLinks || [];
		if(pl.length){ pl.removeClass('active').slice(this.index,this.index+1).addClass('active'); }
	},
	setBtnStatus:function(){
		if( this.index == 0 ){ this.btn_prev.addClass('disabled');this.btn_next.removeClass('disabled'); if(this.total == 1){this.btn_next.addClass('disabled')} }
		else if(this.index >= this.total -1 ){ this.btn_next.addClass('disabled');this.btn_prev.removeClass('disabled'); }
		else { this.btn_prev.add(this.btn_next).removeClass('disabled'); }
	},
	reInit: function(sliderIndex){
		var n = sliderIndex || this.getIndex();
		this.total = Math.ceil(this.items.length / this.row);
		this.scroller.width( this.total * this.itemW );
		if( n === 'first' ){ n = 0 }
		this.goTo(n);
		if( this.pagingNav.length ) { this.initPaging(); }
		this.setBtnStatus();
	}
}

// global functions ----------------------------------------- 

function scrollTo(id,speed){
	if(!$(id).length){ return }
	var speed = speed || 600;
	jQuery("html, body").animate({scrollTop: jQuery(id).offset().top}, speed);
}
function hoverFade(obj) {
	obj.hover(function(){$(this).stop().animate({opacity:0.7},400);},
	function(){	$(this).stop().animate({opacity:1},200); })
}

var sendForm = function(props){
	$.ajax({
		url:props.url,
		type: 'POST',
		data: props.data,
		success: function(data){ $('#feedback').html(data).addClass('active'); }
	});
	return false;
}

validate = function(obj){
	var errors = [];
	obj.each(function(i){
		var t = $(this), row = t.parents('div.row:first');
		switch(t.attr('type')){
			case 'text': 
				if(t.attr('name') === 'email'){
					var x = t.val(), atpos = x.indexOf("@"), dotpos = x.lastIndexOf(".");
					if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { add(t,row); }
					else { remove(t,row); }
				} else {
					!t.val() ? add(t,row) : remove(t,row);
				}
				break;
			case 'checkbox': !t.is(':checked') ? add(t,row) : remove(t,row); break;
		}														
	});
	function add(ele,row){ errors.push(ele.attr('name'));
		row.addClass('error')
		ele.blur( function(){ if(validate($(this))){ $(this).parent().removeClass('error') } } );
	}
	function remove(ele,row){ row.removeClass('error'); }
	if( errors.length ){ return false } else { return true }
}

function getClient() {
	var agent = navigator.userAgent;
	if ( (agent.match(/iPhone/i)) || (agent.match(/iPad/i)) || (agent.match(/iPod/i)) ) {
		return 'iPhone';
	}
	else if ( agent.match(/MSIE 7/i) ) {
		return 'IE7';
	}
	else if ( agent.match(/MSIE 8/i) ) {
		return 'IE8';
	}
	else if ( agent.match(/MSIE 6/i) ) {
		return 'IE6';
	} 
	else {
		return agent;
	}
}

function rangeTable(id){
	$.colorbox({html:$(id).html(),width:700,close:''});
}
