$(document).ready(function(){

	jQuery(document).ready(function() {
		jQuery('#mycarousel').jcarousel();
	});
	
	$(".next").hover(function(){
		$(".film-strip", this).stop().animate({left:'-260px'},{queue:false,duration:300});
	}, function() {
		$(".film-strip", this).stop().animate({left:'0px'},{queue:false,duration:300});
	});
	
		/* Function to draw the background design element */
		
	function drawBackground() {
		
		var vectorDesign = new Array();
		var shapeConstructor = new Object();
			shapeConstructor.maxWidth = $(document).width();
			shapeConstructor.maxHeight = $(document).height();
			shapeConstructor.xStart = shapeConstructor.maxWidth * 1.2;
			shapeConstructor.yStart = 0;
			shapeConstructor.xOffset = 200;
				
		function scewShape() {
			scewDegrees = 45;
			scewRadian = scewDegrees * Math.PI / 180;
			scewOffset = Math.tan(scewRadian) * shapeConstructor.maxHeight;
			string = 'm ' + shapeConstructor.xStart + ' ' + shapeConstructor.yStart + ' ' +
					'l ' + '-'+ scewOffset + ' ' + shapeConstructor.maxHeight + ' ' +
					'L ' + '0 ' + shapeConstructor.maxHeight + ' ' +
					'L ' + '0 0 z';
			shapeConstructor.xStart -= shapeConstructor.xOffset;
			return string;
		}
		
		paper = Raphael(0, 0, shapeConstructor.maxWidth, shapeConstructor.maxHeight);
		vectorDesign.push(paper.rect(0, 0, shapeConstructor.maxWidth, shapeConstructor.maxHeight).attr({'fill': '90-#E2001A-#EC7552', 'stroke': 'none'}));
		vectorDesign.push(paper.path(scewShape()).attr({'fill': '90-#FFED00-#FFFDEA', 'stroke': 'none'}));
		vectorDesign.push(paper.path(scewShape()).attr({'fill': '90-#FFFFFF-#FFFFFF', 'stroke': 'none'}));
		vectorDesign.push(paper.path(scewShape()).attr({'fill': '90-#02184C-#005CA7', 'stroke': 'none'}));
		vectorDesign.push(paper.path(scewShape()).attr({'fill': '90-#009EE0-#C4E7F8', 'stroke': 'none'}));
		
		return paper.canvas;
	}
	
	function redrawBackground() {
		backgroundObject.remove();
		backgroundObject = $(drawBackground());
	}
	
	/* Runs AFTER window is resized Ref. http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-fire-after-the-resize-is-completed */
	
	var delay = (function(){
		var timer = 0;
		return function(callback, ms) {
			clearTimeout (timer);
			timer = setTimeout(callback, ms);
		};
	})();
	
	$(window).resize(function() {
		delay(function(){
			redrawBackground();
		}, 500);
	});

	/* Animation of top navigation */

	$('.top-container.ghost').css({
		'top': function() {
			return '-' + $(this).height() + 'px';
		}
	}).delay(1000).animate({
		'top': '0px'
	}, 300, 'easeOutBounce');
	
	$('.row .column:not(:last-child)').addClass('ie-not-last');
	
	if($.browser.msie && Math.round($.browser.version, 0, 1) == 6) {
		DD_belatedPNG.fix('.trinket span');
	}
	
	backgroundObject = $(drawBackground());
	$(window).load(
		function() {
			redrawBackground();
		}
	);
});


$(document).ready(function(){
	$('ul.leftnav > ul').hover(function(){
		
	}, function() {
		$(this).fadeOut(500);
	});
});

function upload_template_img(template_id) {
	xferWindow = window.open('/admin/includes/php/upload_template_img.php?template_id='+template_id+'','popWin','toolbar=0,status=0,height=300,width=360');
	xferWindow.opener = self;
}

function upload_ot_template_img(template_id) {
	xferWindow = window.open('/admin/includes/php/upload_ot_template_img.php?template_id='+template_id+'','popWin','toolbar=0,status=0,height=300,width=360');
	xferWindow.opener = self;
}

function open_window(psrc, popW, popH) {
	var left = parseInt((screen.availWidth/2) - (popW/2));
  var top = parseInt((screen.availHeight/2) - (popH/2));
	xferWindow = window.open(''+psrc+'','popWin','scrollbars=yes,toolbar=0,status=0,height='+popH+',width='+popW+',top='+top+',left='+left+',screenX='+left +',screenY='+ top+'');
	xferWindow.opener = self;
}

function show_hide_div(div_id) {
	if(document.getElementById(div_id).style.display == 'none') {
		document.getElementById(div_id).style.display = 'block';
	} else {
		document.getElementById(div_id).style.display = 'none';
	}
}

function confirmDelete(goto) {
	if(confirm("Delete Item?")) {	
		if(goto != "") {
			location.href = goto;
		} else {	
			return true;
		}
	} else {
		return false;
	}
}

function confirmDeleteAjax(vars, goto, divid) {
	if(confirm("Delete Item?")) {	
		if(goto != "") {
			 send_request(''+vars+'',''+goto+'?'+vars+'',''+divid+'');
		} else {	
			return true;
		}
	} else {
		return false;
	}
}

function doAjax(vars, goto, divid) {
	send_request(''+vars+'',''+goto+'?'+vars+'',''+divid+'');
}

function serialize( mixed_value ) {
    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") {
      val += ";";
  }
    return val;
}

function change_class(divid, classname) {
	document.getElementById(divid).className = classname;
}

function positionSort(param, table, primfield, find) {
	var i = 0;
	$(param).find(''+find+'').each(function() {
		i++;
		val = this.value;
		doAjax('t='+table+'&id='+val+'&pos='+i+'&primf='+primfield,'/admin/includes/ajax/pos_change.php','hiddiv');
	});
}
