﻿/******************************************************************************
 **                  MEDIAGISTIC FUNCTIONS JS v4.CE.r100629                  **
 **                 ©2010 Mediagistic.  All rights reserved.                 **
 **                    http://www.mediagisticpageone.com                     **
 *****************************************************************************/

$(document).ready(function(){
	mgPageOne=new mgPageInfo();
	mgGo();
});

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object();
	this.get=Querystring_get;

	if (qs == null)
		qs=location.search.substring(1,location.search.length);

	if (qs.length == 0) return;

	qs = qs.replace(/\+/g, ' ');

	var args = qs.split('&'); // parse out name/value pairs separated via &
	
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=');
		var name = unescape(pair[0]);

		if (pair.length == 2)
			value = unescape(pair[1]);
		else
			value = name;
		
		this.params[name] = value;
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key];
	if (value==null) value=default_;
	
	return value;
}

function getArgs() {
	var args = new Object(); 
	var query = location.search.substring(1); 
	var pairs = query.split("&"); 
	for(var i = 0; i < pairs.length; i++) 
	{ 
		var pos = pairs[i].indexOf('='); 
		if (pos == -1) continue; 
		var argname = pairs[i].substring(0,pos); 
		var value = pairs[i].substring(pos+1); 
		args[argname] = unescape(value); 
	} 
	return args; 
}

function QueryDataCheck(){
	var args = getArgs(); 
	if (args.a){
		a = args.a; 
		document.getElementById("adGroup").value = a;
		revealDiv(a);
	} else {
		a = '';
	}

	if (args.goTo){
		mgPageOne.toggleProductDetail(args.goTo,'slide','fast','74','238');
		mgPageOne.scrollPageTo(args.goTo,-130)
	}
	mgPageOne.qdataCount++;
}


function MG_showHide(layer_ref) {
	divStatus = document.getElementById(layer_ref).style.display;
	if (divStatus != 'block') {
		document.getElementById(layer_ref).style.display = 'block';
	} else {
		document.getElementById(layer_ref).style.display = 'none';
	}
}


function mgGo() {
	QueryDataCheck();
}


function mgPageInfo() {
	this.configCount=0;
	this.qdataCount=0;

	this.Path = new function() {
		this.rawURL = window.location.href;
		this.protocol = "";
		this.host = "";
		this.virtualPath = "";
		this.virtualPathClean = "";
		this.queryString = "";
		this.fileName = "";
		this.isHome = false;
		
		var tmp;
		tmp=this.rawURL.split("://");
		this.protocol = tmp[0];						//the URL protocol (http|https)
		this.virtualPath = tmp[1];					//so far, the full URL, without http(s)://
		tmp = this.virtualPath.split("/");			
		this.host = tmp[0];							//the host portion of the URL
		this.virtualPath = this.virtualPath.replace(this.host,"");	//vurl cleaned into true virtual path (minus leading slash)
		if (this.virtualPath.search(/\?/) != -1) {	//qs detected
			tmp = this.virtualPath.split("?");
			this.virtualPathClean = tmp[0];			//virtual path without QS
			this.queryString = tmp[1];				//the full querystring (after "?")
		} else {
			this.virtualPathClean = this.virtualPath;
			this.queryString = "";
		}
		// get filename from vurl_noqs
		tmp = this.virtualPathClean.match(/[a-z0-9_]{1,}\.[a-z0-9_\#]{2,}$/gi);
		if (tmp == null) {
			this.fileName = "default_document";
		} else {
			this.fileName = tmp[0];
		}
		if (this.fileName.search(/[default|index|home]|[internet_specials]/gi) != -1) {
			this.isHome = true;
		} else {
			this.isHome = false;
		}
	}
}
	
mgPageInfo.prototype.showHide = function(tgt,effect,speed) {
	var target = $("#"+tgt);
	var fxtype = 'fade', fxtime = 'fast',h,w,indA,indB,theHtml;
	var hasInd = false;
	if (target.siblings(".mgIndExpand")){
		hasInd = true;
		indA = target.siblings(".mgIndExpand");
		indB = target.siblings(".mgIndCollapse");
	}

	if (effect) {
		fxtype=effect;
	}
	if (speed) {
		fxtime=speed;
	}

	if (target.is(":visible")){
		if (fxtype == "slide") {
			target.slideUp(fxtime);
		} else {
			target.fadeOut(fxtime);
		}
	} else {
		target.css("display","block");
		h = target.height() + "px";
		target.css("height", h);		
		target.css("display","none");
		target.hide();
		if (fxtype == "slide") {
			target.slideDown(fxtime);
		} else {
			target.fadeIn(fxtime);
		}
	}
	if (hasInd == true) {
		if (indA.is(":visible")){
			indA.hide();
			indB.show();
		} else {
			indB.hide();
			indA.show();
		}
	}
	if (target.siblings(".mgIndOther")){
		var otherInd = target.siblings(".mgIndOther");
		if (otherInd.is(":hidden")) {
			otherInd.show();
		} else {
			otherInd.hide();
		}
	}
}
mgPageInfo.prototype.swapDiv = function(tgtDivA,tgtDivB,baseDiv) {
	var divA,divB,useBase = "";
	if (baseDiv) {
		useBase = baseDiv;
	}
	divA = $("#"+useBase+tgtDivA);
	divB = $("#"+useBase+tgtDivB);

	if (div1.is(":visible")) {
		div1.hide();
		div2.show();
	} else {
		div2.show();
		div1.hide();
	}
}
mgPageInfo.prototype.prodImgZoom = function(tgtId,smSize,lgSize) {
	var tgt = $("#"+tgtId);
	var isSquare,tgtSize=[],wHsm=[],wHlg=[],i;

	if (smSize.search(/x/i) != -1) {
		isSquare = false;
		wHsm = smSize.split("x");
		sHlg = lgSize.split("x");
	} else {
		isSquare = true;
		for (i=0;i<2;i++){
			wHsm[i] = smSize;
			wHlg[i] = lgSize;
		}
	}
	if (tgt.width() == smSize){
		tgtSize[0] = parseInt(wHlg[0]);
		tgtSize[1] = parseInt(wHlg[1]);
	} else {
		tgtSize[0] = parseInt(wHsm[0]);
		tgtSize[1] = parseInt(wHsm[1]);
	}
	
	tgt.animate({
		width: tgtSize[0], height: tgtSize[1]
		}, 400);
}
mgPageInfo.prototype.toggleProductDetail = function(productId,effect,speed,smImgSize,lgImgSize) {
	var tgtImg = "img-"+productId;
	mgPageInfo.prototype.showHide(productId,'slide','fast');
	mgPageInfo.prototype.prodImgZoom(tgtImg,smImgSize,lgImgSize);
}
mgPageInfo.prototype.scrollPageTo = function(tgtElementById, offsetPx) {
	var tgt = $("#"+tgtElementById);
	var tgtY = tgt.offset();
	if (!offsetPx){offsetPx=0;}
	var adjScroll = tgtY.top+offsetPx;
	$('html, body').animate({
			scrollTop: adjScroll
			},500);
}


