
$(document).ready(function() {

    active = $("#media_content li.active");
    maxWidth = 851;
    minWidth = 41;

    $('#media_content li').toggle(function() {
        $(active).animate({ width: minWidth + "px" }, { queue: false, duration: 500 });
        $(this).animate({ width: maxWidth + "px" }, { queue: false, duration: 500 });
        active = this;
    }, function() {

        $(active).animate({ width: minWidth + "px" }, { queue: false, duration: 500 });
        $(this).animate({ width: maxWidth + "px" }, { queue: false, duration: 500 });
        active = this;
    });

});



$('#remote').toggle(function() {
    $('#media_content').cycle('pause');
    $(this).attr({ src: "/resources/img/play.png", title: "spela bildspelet" });

}, function() {

    $('#media_content').cycle('resume', true);
    $(this).attr({ src: "/resources/img/pause.png", title: "pausa bildspelet" });
});

/*********** Bildspel med rubrik och text *******************/

/*********** Bildspel med rubrik och text *******************/
function CreateMediaContent(mediaContentWrapper, options) {
    $(mediaContentWrapper)
    //.after('<div id="nav">') 
	.cycle({
	    fx: 'fade',
	    speed: '1000',
	    //delay:  -3000,
	    timeout: options.timeout != null ? options.timeout : 9000,
	    pager: '#nav',
	    pagerEvent: 'click',
	    pause: true,
	    pauseOnPagerHover: true,
	    //next:   '#next', 
	    //before:  onBefore, 
	    //after:   onAfter,
	    //slideExpr: 'li'
	    cleartype: 1,
	    cleartypeNoBg: 1,
	    pagerAnchorBuilder: function(idx, slide) {
	        // return selector string for existing anchor
	        return '#nav a:eq(' + idx + ') ';
	    }
	});
};

/*********** Nyhetsnurra *******************/
function CreateNewsSlider(newsSlider, options) {
    $(newsSlider)
	.cycle({
	    fx: 'scrollUp', //fade
	    speed: 1500,
	    easing: 'easeOutExpo', //easeOutBounce, easeInBack 
	    timeout: options.timeout != null ? options.timeout : 0,
	    pager: '.menu',
	    pagerEvent: 'click',
	    pause: true,
	    pauseOnPagerHover: true,
	    prev: '.news_prev',
	    next: '.news_next',
	    cleartype: true,
	    cleartypeNoBg: true,
	    pagerAnchorBuilder: function(idx, slide) {
	        //return selector string for existing anchor
	        return '.menu li a:eq(' + idx + ')';
	    }
	});
};

//function CreateMediaContent(mediaContentWrapper, options) {
//    $(mediaContentWrapper)
//	.after('<div id="nav">')
//	.cycle({
//	    fx: 'turnLeft',
//	    speed: '1000',
//	    timeout: options.timeout != null ? options.timeout : 9000,
//	    pager: '#nav',
//	    pagerEvent: 'mouseover',
//	    pause: true,
//	    pauseOnPagerHover: true,
//	    cleartype: true,
//	    cleartypeNoBg: true
//	});
//};



/*************************************************************
************ Horizontell accordion ***************************
*************************************************************/
function CreateHrzAccordion(hrzAccordion, options) {
    $(hrzAccordion).hrzAccordion({
        //--OPTION----------------------DEFAULT-------------------------------------------------------------------------------------------------------------------------------------
        eventTrigger: "click", 						    // mouseover, click
        containerClass: "hrzAccordion", 				// The div container wrapped around the whole list.
        listItemClass: "listItem", 						// Class assigned to each list item
        contentContainerClass: "content_container", 	// Class that defines the actual container for the content that will be animated
        contentWrapper: "content_wrapper", 			    // A container wrapped around the content to apply special layout definitions
        contentInnerWrapper: "content", 		 		// Another inner container wrapped around the content
        handleClass: "handle", 						    // Class that defines the look of the handle, this handles the open and close event for each blind
        handleClassOver: "handle_over", 				// Class that defines the mouse over event for the handle
        handleClassSelected: "handle_selected", 		// Class that defines the select status of the handle
        handlePosition: "left", 						// Position of the handle (left | right | bottom | top)
        closeSpeed: 1000, 							    // The closing speed
        openSpeed: 1000, 							    // The opening speed
        openOnLoad: 1, 							        // Define which container to show on page load. (false, 1, 2, 3, ....)
        hashPrefix: "tab", 						        // Allows you to open containers using a hash tag in the url (index.html#tab1). 
		//eventAction: function(){ 					 	//add your own extra clickAction function here
			//},
		//completeAction: function(){ 				 	//add your own onComplete function here
		//	},
        closeOpenAnimation: 1, 							// 1 - open and close at the same time
        cycle: true, 							        // Cycle through containers by interval
        cycleInterval: 20000, 						    // Cycle interval in milliseconds (10000 = 10 seconds)
        fixedWidth: "775", 						        // Use fixed width for containers. Normally the plugin calculates the width from the main container.
        eventWaitForAnim: true							// Makes sure to allow no new event to be triggered, until animation has ended
    });
};

/*************************************************************
************ Accordian ***************************************
*************************************************************/
function CreateAccordion(containerSelector, containerLinkSelector) {
    $(containerLinkSelector).click(function() {

        /*********** Dynamisk marginal ******************/
        $('.accordian li.selected').animate({ marginTop: "0" }, 'medium')
        if (!$(this).hasClass('selected')) {
            $(this).parent().animate({ marginTop: "10" }, 'medium')
        }
    }, function() {
        $(this).parent().animate({ marginTop: "0" }, 'medium')
        $('.accordian li.selected').animate({ marginTop: "0" }, 'medium')
    });

    /*********** Allmäna egenskaper *************************/
    $(containerSelector).accordion({
        header: 'h3',
        navigation: false,
        active: '.selected',
        autoheight: false,
        clearStyle: true,
        collapsible: true,
        alwaysOpen: false,
        animated: 'slide'
    });
};

/*************************************************************
************ Andra JS ****************************************
*************************************************************/

/*********** Searcbox ***************************************/
$(function() {
    swapValues = [];
    $(".swap_value").each(function(i) {
        swapValues[i] = $(this).val();
        $(this).focus(function() {
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function() {
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i]);
            }
        });
    });
});

/*********** Externa länkar *********************************/
function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}
window.onload = externalLinks;

function toggle_visibility(part) {
    var e = document.getElementById(part);
    if (e.style.display == 'none') {
        e.style.display = 'inline';
    }
    else {
        e.style.display = 'none';
    }
}

/*********** Öppna/stäng varukorg ****************************/
function ShowHideCart() {
    if ($('#cart_info').is(':hidden')) {
        $('#cart_info').slideDown('medium');
        $('#cart_trigger').attr({ innerHTML: "St&auml;ng varukorg", title: "St&auml;ng varukorg" });
    }
    else {
        $('#cart_info').slideUp('medium');
        $('#cart_trigger').attr({ innerHTML: "Visa varukorg", title: "Visa varukorg" });
    }
}

/*********** Popup *******************************************/
function ShowFancybox(headline, text, cssclass) {
    $h3 = $('<h3/>').append(headline);
    //    $par = $('<p/>').append(text);
    $cont = $('<div/>').append($h3).append(text);
    $('#fancybox_content').empty().css('display', 'block').addClass(cssclass).append($cont);

    $('#fancybox_trigger').fancybox({
        'hideOnContentClick': false,
        onClosed: function() {
            $('#fancybox_content').hide().removeClass();
        }
    }).trigger('click');
}

function ShowFancyboxNoClosing(headline, text, cssclass) {
    $h3 = $('<h3/>').append(headline);
    $cont = $('<div/>').append($h3).append(text);
    $('#fancybox_content').empty().css('display', 'block').addClass(cssclass).append($cont);

    $('#fancybox_trigger').fancybox({
        'hideOnContentClick': false,
        'showCloseButton': false,
        'enableEscapeButton': false,
        'hideOnOverlayClick': false,
        onClosed: function() {
            $('#fancybox_content').hide().removeClass();
        }
    }).trigger('click');
}

function PrepareFancybox(cssclass) {
    $cont = $('<div/>').append($('<img/>').attr('alt', 'loading').attr('src', '/resources/img/ajax-loader-big-product.gif'));
    $('#fancybox_content').empty().css('display', 'block').addClass(cssclass).append($cont);
    $('#fancybox_trigger').fancybox({
        'hideOnContentClick': false,
        onClosed: function() {
            $('#fancybox_content').hide().removeClass();
        }
    }).trigger('click');
    return $('#fancybox_content');
}

function HideFancyboxText() {
    $.fancybox.close();
}

function ShowFancyboxAndFade(headline, text, delay, cssclass) {
    ShowFancybox(headline, text, cssclass);
    setTimeout('HideFancyboxText();', delay);
}

function ShowLoader() {
    // visa ajax loader
}

function HideLoader() {
    // dölj ajax loader
}

function CreateULErrorList(ulclass, liclass, errors) {
    var list = $('<ul/>').addClass(ulclass);

    $.each(errors, function() {
        list.append($('<li/>').addClass(liclass).append(this.toString()));
    });

    return list;
}

function CreateULList(ulclass, liclass, listOfStringItems) {
    var list = $('<ul/>').addClass(ulclass);

    $.each(listOfStringItems, function() {
        list.append($('<li/>').addClass(liclass).append(this.toString()));
    });

    return list;
}

function OverrideEnterKeypress(input, anchor) {
    $(input).keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $(anchor).click();
            return false;
        } else {
            return true;
        }
    });
}

function AnimateBackground(element, fadeColor, fadeTime, fadeBackColor, fadeBackTime) {
    $(element).animate({ backgroundColor: fadeColor }, fadeTime, 0, function() { $(element).animate({ backgroundColor: fadeBackColor }, fadeBackTime); });
}

function isInteger(value) {
    if (value.match(/^\d+$/) == null)
        return false;
    else
        return true;
}

function isFloat(value) {
    if (value.match(/^[-+]?[0-9]+(\.[0-9]+)?$/) == null)
        return false;
    else
        return true;
}
